php 锚点
Last week, to create a PHP script that finds anchors in an HTML document and assigns an ID to the element IF the element doesn't have an ID already. stepped up to the challenge.
上周, ,要求他们创建一个PHP脚本,该脚本可在HTML文档中找到锚点,如果该元素还没有ID,则为该元素分配一个ID。 ( 加紧了挑战。
PHP (The PHP)
function anchor_fix($anchor){ // the match comes as an array // the whole match (what we want) is the 0th element if (! preg_match('/\sid="/i', $anchor[0])) { return preg_replace('/name="([^"]*)"/i', 'id="$1" $0', $anchor[0]); } else { // already has an id! return $anchor[0]; }}/* usage */echo preg_replace_callback('/ ]*>/i', 'anchor_fix', file_get_contents('page.html'));
结果 (The Result)
this is an anchor some text... another one...
... becomes ...
...变成...
this is an anchor some text... another one...
Great job Jeremy!
杰里米(Jeremy)做得好!
翻译自:
php 锚点