PHP中文件中的大写链接
仍然在学习PHP基础知识的路上。ATM,我正试图找到一种方法来“突出显示”文件中的链接描述 比方说,如果我有PHP中文件中的大写链接,php,css,hyperlink,uppercase,Php,Css,Hyperlink,Uppercase,仍然在学习PHP基础知识的路上。ATM,我正试图找到一种方法来“突出显示”文件中的链接描述 比方说,如果我有 <body>Hello World <a href=http://web.com title="link">This is a link</a> 你好,世界 它会变成 <body>Hello World <a href=http://web.com title="LINK">THIS IS A LINK</a>
<body>Hello World <a href=http://web.com title="link">This is a link</a>
你好,世界
它会变成
<body>Hello World <a href=http://web.com title="LINK">THIS IS A LINK</a>
你好,世界
到目前为止,我只使用此代码替换了一个部件
<?php
$matches = file_get_contents($argv[1]);
preg_match('/=".*a>/', $matches, $links);
print_r($links);
?>
不要在PHP中执行此操作。这是一个设计决策,因此应使用CSS:
a {
text-transform: uppercase;
}
如果你真的必须用PHP来做,你可以使用,它允许你编写自己的函数来处理替换。这可能是这样的:
<?php
$string = '<body>Hello World <a href=http://web.com title="link">This is a link</a>';
$string = preg_replace_callback("/(<a.+title=[\"'])(.+?)([\"']>)([^<>]+?)(<\/a>)/", "callback_strtolower", $string);
echo $string;
function callback_strtolower($matches) {
var_dump($matches);
return $matches[1] . strtoupper($matches[2]) . $matches[3] . strtoupper($matches[4]) . $matches[5];
}
?>
锚文本:css规则
a {
text-transform: uppercase;
}
属性标题的文本(不知您是否会使用PHP进行设置):
当然css在这里更合适,但是标题
文本呢?他们也想把它改成upper。我必须用php来做,这是个棘手的问题。。。我曾想过将preg_match或preg_replace与strtoupper结合使用,但这似乎不起作用
$dom = new DOMDocument();
$dom->loadHTML($string);
$links = $dom->getElementsByTagName("a");
foreach ($links as $link) {
foreach ($link->childNodes as $child) {
if ($child instanceof DOMText)
$link->replaceChild(new DOMText(strtoupper($child->wholeText)), $child);
}
if ($link->hasAttribute("title")) {
$link->setAttribute("title", strtoupper($link->getAttribute("title")));
}
}
echo $dom->saveHTML();
a {
text-transform: uppercase;
}
$myTitleInPhp = strtoupper($myTitleInPhp );