我正在尝试从数据库中检索到的HTML中第n个</p>
标记后插入一些html。
我发现this php code适用于常规文本,但似乎不适用于</p>
。
function str_replace_n($search, $replace, $subject, $occurrence) {
$search = preg_quote($search,'/');
return preg_replace("/^((?:(?:.*?$search){".--$occurrence."}.*?))$search/", "$1$replace", $subject);
}
曾经试图使它起作用,但一直未能做到。
我正在使用以下调用来调用该函数:
$row['post_content'] = $this->str_replace_n("</p>", "<a href='https://www.google.com/'>test</a></p>",$this->test_html() , 2);
test_html()返回的html是:
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis mattis in dolor at sodales. Donec nec pellentesque ipsum. Nullam a velit in neque volutpat rhoncus t
incidunt nec nisl. Suspendisse sem enim, tincidunt ac orci vel, sodales gravida metus. Duis dignissim urna ac porttitor imperdiet. Morbi in cursus nisl, vitae molest
ie ipsum. Suspendisse non ipsum suscipit, congue mauris ac, condimentum magna. Vivamus ultricies suscipit magna, sed finibus justo vestibulum sed.</p>
<p>Duis varius ultrices diam eu facilisis. Aenean non auctor nibh, vel blandit enim. Maecenas porta tellus at purus efficitur rutrum. Proin ullamcorper metus semper
tempus consectetur. Nullam vel pellentesque elit. Praesent fringilla libero elit, non blandit purus placerat eget. Nunc eu urna a est pretium convallis ac eget nunc.
Duis laoreet turpis sed sapien molestie, in ornare est lobortis.</p>
<p>Donec scelerisque consequat facilisis. Vestibulum at dignissim neque. Aenean tincidunt nulla in eros aliquet mollis. Fusce lacinia vehicula aliquet. Pellentesque
vitae orci eget purus suscipit suscipit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vestibulum auctor, augue at vest
ibulum placerat, risus lacus sodales neque, non pretium leo velit vel est. Phasellus lacinia maximus eros, viverra finibus augue gravida pharetra. Integer id ferment
um ex.</p>
<p>Sed suscipit, massa ac laoreet auctor, justo ipsum congue sapien, at gravida nulla risus non erat. Donec varius vehicula velit, nec volutpat enim feugiat a. Sed t
empor finibus maximus. Sed mauris ipsum, dictum quis felis non, blandit sollicitudin purus. In a diam non sapien mattis cursus. In pharetra euismod finibus. Integer
placerat eu lorem in maximus.</p>
<p>Mauris ut dolor id lectus laoreet commodo. Integer venenatis blandit augue eget faucibus. Sed euismod dui mi, at congue turpis imperdiet ut. Proin vulputate ferme
ntum porttitor. Nulla facilisi. Mauris sollicitudin lectus elit, id ultricies ipsum vulputate nec. Maecenas interdum, arcu sit amet facilisis consequat, turpis liber
o blandit dolor, sed suscipit leo felis quis felis. Curabitur varius quam at leo dapibus suscipit.</p>
任何帮助将不胜感激。
关于重复的问题,我不同意。答案链接虽然提供了很多信息,但并未提供有关如何完成我要实现的目标的任何实质性信息。
答案 0 :(得分:1)
您的正则表达式有几个问题。首先,您已使用^
将正则表达式锚定到字符串的开头,因此最多只能有一个匹配项。其次,您需要提供s
修饰符,以便.
可以匹配换行符和空格。尝试使用此版本的str_replace_n
函数:
function str_replace_n($search, $replace, $subject, $occurrence) {
$search = preg_quote($search,'/');
return preg_replace("/((?:(?:.*?$search){".--$occurrence."}.*?))$search/s", "$1$replace", $subject);
}
示例操作:
$html= '<p>Lorem ipsum </p>
<p>Duis varius ultrices.</p>
<p>Donec scelerisque .</p>
<p>Sed suscipit, massa.</p>
<p>Mauris ut dolor id</p>';
echo str_replace_n("</p>", "<a href='https://www.google.com/'>test</a></p>",$html , 2);
输出:
<p>Lorem ipsum </p>
<p>Duis varius ultrices.<a href='https://www.google.com/'>test</a></p>
<p>Donec scelerisque .</p>
<p>Sed suscipit, massa.<a href='https://www.google.com/'>test</a></p>
<p>Mauris ut dolor id</p>
该函数的版本还旨在在字符串的第n次出现之前 之前插入文本。如果要在第n次出现后的 之后插入新文本,则需要将功能更改为:
function str_replace_n_after($search, $replace, $subject, $occurrence) {
$search = preg_quote($search,'/');
return preg_replace("/((?:.*?$search){".$occurrence."})/s", "$1$replace", $subject);
}
echo str_replace_n_after("</p>", "<a href='https://www.google.com/'>test</a>",$html , 2);
输出:
<p>Lorem ipsum </p>
<p>Duis varius ultrices.</p><a href='https://www.google.com/'>test</a>
<p>Donec scelerisque .</p>
<p>Sed suscipit, massa.</p><a href='https://www.google.com/'>test</a>
<p>Mauris ut dolor id</p>