Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 替换最后出现的<;p>;用字符串标记_Php_Regex_Preg Replace - Fatal编程技术网

Php 替换最后出现的<;p>;用字符串标记

Php 替换最后出现的<;p>;用字符串标记,php,regex,preg-replace,Php,Regex,Preg Replace,我希望替换字符串中最后出现的p标记 $bodytext = preg_replace(strrev("/<p>/"),strrev('<p class="last">'),strrev($bodytext),1); $bodytext = strrev($bodytext); 有些人会抱怨DOMDocument在解析HTML时比正则表达式更冗长。但是冗长是可以的,如果它意味着使用正确的工具来完成工作 $previous_value = libxml_use_intern

我希望替换字符串中最后出现的p标记

$bodytext = preg_replace(strrev("/<p>/"),strrev('<p class="last">'),strrev($bodytext),1);
$bodytext = strrev($bodytext);

有些人会抱怨DOMDocument在解析HTML时比正则表达式更冗长。但是冗长是可以的,如果它意味着使用正确的工具来完成工作

$previous_value = libxml_use_internal_errors(TRUE);
$string = '<p>hi, mom</p><p>bye, mom</p>';
$dom = new DOMDocument();
$dom->loadHTML($string);
$paragraphs = $dom->getElementsByTagName('p');
$last_p = $paragraphs->item($paragraphs->length - 1);
$last_p->setAttribute("class", "last");
$new_string = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $dom->saveHTML()));
libxml_clear_errors();
libxml_use_internal_errors($previous_value);

echo htmlentities($new_string);
// <p>hi, mom</p><p class="last">bye, mom</p>
$previous\u value=libxml\u use\u internal\u错误(TRUE);
$string='嗨,妈妈

再见,妈妈

'; $dom=新的DOMDocument(); $dom->loadHTML($string); $parages=$dom->getElementsByTagName('p'); $last_p=$段落->项目($段落->长度-1); $last_p->setAttribute(“class”,“last”); $new_string=preg_replace('/^/','',str_replace(数组('','',''),数组('','',''),$dom->saveHTML()); libxml_clear_errors(); libxml_使用_内部_错误($previous_值); echo htmlentities($new_string); //嗨,妈妈

再见,妈妈

使用如何

require_once('simple_html_dom.php');
$string='嗨,妈妈

再见,妈妈

'; $doc=str\u get\u html($string); $doc->find('p',-1)->class='last'; echo$doc; //嗨,妈妈

再见,妈妈


。使用DOM代替,查看。如果你这样做,你可以调整最后的<代码> P<代码>,你应该考虑使用<代码>:最后的< /代码>。这是我首先看到的,但我仍然想支持IE 7和8。我欣赏代码示例。我将查看并测试解析速度结果-但考虑到字符串总是在500个字符左右,并且只包含一个链接和两组

标记,这似乎太过分了。它会很快。不要把速度和代码行联系起来。还有唐;不要让速度成为唯一的因素。正则表达式不是用来处理HTML的。DOMDocument是。我修改了我的帖子-如果我使用了你的代码的缩短版本,会有任何问题吗?不会有任何问题,除非你传递的字符串在任何时候包含无效的HTML。然后您将得到由PHP生成的警告。好的,谢谢,CKEditor正在处理html输出,所以这不应该是一个问题。
$dom = new DOMDocument();
$dom->loadHTML($bodytext);
$paragraphs = $dom->getElementsByTagName('p');
$last_p = $paragraphs->item($paragraphs->length - 1);
$last_p->setAttribute("class", "last");
$bodytext = $dom->saveHTML();
$previous_value = libxml_use_internal_errors(TRUE);
$string = '<p>hi, mom</p><p>bye, mom</p>';
$dom = new DOMDocument();
$dom->loadHTML($string);
$paragraphs = $dom->getElementsByTagName('p');
$last_p = $paragraphs->item($paragraphs->length - 1);
$last_p->setAttribute("class", "last");
$new_string = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $dom->saveHTML()));
libxml_clear_errors();
libxml_use_internal_errors($previous_value);

echo htmlentities($new_string);
// <p>hi, mom</p><p class="last">bye, mom</p>
require_once('simple_html_dom.php');

$string = '<p>hi, mom</p><p>bye, mom</p>';
$doc = str_get_html($string);
$doc->find('p', -1)->class = 'last';
echo $doc;
// <p>hi, mom</p><p class="last">bye, mom</p>