I have to replace the last match of a string (for example the word foo) in HTML document. The problem is that the structure of the HTML document is always random.
我试图以先验的方式做到这一点,但迄今为止,我知道如何只取代第一种匹配,而不是最后的对应办法。
感谢。
I have to replace the last match of a string (for example the word foo) in HTML document. The problem is that the structure of the HTML document is always random.
我试图以先验的方式做到这一点,但迄今为止,我知道如何只取代第一种匹配,而不是最后的对应办法。
感谢。
在<代码>后使用负值(!
$str = text abcd text text efgh ;
echo preg_replace( ~text(?!.*text)~ , bar , $str),"
";
<>光>
text abcd text bar efgh
将所有案文与随后的模式的最后出现相吻合的共同做法是使用greedy do,。 因此,您可在最后<代码>text
之前对案文进行匹配和记录,并以背书+新价值取代:
$str = text abcd text text efgh ;
echo preg_replace( ~(.*)text~su , ${1}bar , $str);
// => text abcd text bar efgh
如果text
是一定价值的变量,这些变量必须作为平原文本处理,则使用preg_quote
至,所有特殊char都正确逃脱:
preg_replace( ~(.*) . preg_quote($text, ~ ) . ~su , ${1}bar , $str)
这里,(*)
compes and captures into Group 1 any 0 or more chars (note that the s
modifier make the dotcomp Line chars, as well),尽量多,以至text
的正确多数(最后)发生。 如果text
is a Unicode substring,u
modifier就在P(HP使(UT*F
)中投放。 PCRE verb允许将即将进行的扼杀作为统法协会编码要点的顺序而不是按字节,以及<代码>(*UCP) verb,从而使统法协会的所有短身类别都知道——如果有的话。
<代码>{1}是替换背书,持有第1组所捕获值的持单人,可在所形成的舱内恢复该舱位。 您可使用<代码>$1,但。 首先是数字。
实例
<?php
$str = Some random text ;
$str_Pattern = /[^ ]*$/ ;
preg_match($str_Pattern, $str, $results);
print $results[0];
?>
当然,这里提出的公认解决办法是正确的。 然而,您也不妨看。 员额。 如果不需要这种模式,则采用这种方式,且体内并不包含无法由所使用职能(即多比特职能)承担的特性。 我也提出了另一个排除/处理案例的参数。
第一行是:
$pos = $case === true ? strripos($subject, $search) : strrpos($subject, $search);
我必须承认,我没有测试业绩。 然而,我猜测,preg_replace()较慢,特别是在大范围铺上。
I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...
<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...
I found this script online that creates a thumbnail out of a image but the thumbnail image is created with poor quality how can I improve the quality of the image. And is there a better way to create ...
如何确认来自正确来源的数字。
Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...
I wonder there is a way to post a message to a facebook business page with cURL? thanks
I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...
How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...