str_replace 直到 PHP 中的字符串结尾的正确方法是什么?
What is the correct way to str_replace until the end of string in PHP?
如果我有这样的字符串:
<a href="http://example.com/myPDF.pdf" rel="">myPDF12345431234</a>
什么是对 str_replace 的正确 PHP 函数调用,以便“ rel=....” 之后的所有内容都被 nothing/blank 替换?
为进一步说明,link 标记之间的值是动态且未知的,因此我无法调用它。
我正在寻找类似的东西:
$oldstring = array('<a href="', '" rel="" *until the end_of_string* ');
$replacewith = array=('', '');
$newstring = str_replace($oldstring, $replacewith, $URL);
将 URL(直到字符串末尾的引号)之后的所有内容都替换掉的正确方法是什么?
这是由正则表达式处理的。以下内容替换了从 rel="" 到字符串末尾的所有内容。
$newstring = preg_replace('/ rel="".*$/','',$oldstring);
.* 表示 "everything",$ 表示 "end of string"。我在 rel 之前添加了一个 space,因为我假设您也想删除它。
听起来您真正要查找的是当前字符串的 子字符串 ,直到 rel
属性的末尾。
$newstring = substr($oldstring, 0, strpos($oldstring, 'rel=""'));
使用preg_match:
preg_match("/(\<a href\=\".*\")\srel/", "<a href="http://example.com/myPDF.pdf" rel="">myPDF12345431234</a>", $output_array);
Var_dump($output_array);
如果我有这样的字符串:
<a href="http://example.com/myPDF.pdf" rel="">myPDF12345431234</a>
什么是对 str_replace 的正确 PHP 函数调用,以便“ rel=....” 之后的所有内容都被 nothing/blank 替换?
为进一步说明,link 标记之间的值是动态且未知的,因此我无法调用它。
我正在寻找类似的东西:
$oldstring = array('<a href="', '" rel="" *until the end_of_string* ');
$replacewith = array=('', '');
$newstring = str_replace($oldstring, $replacewith, $URL);
将 URL(直到字符串末尾的引号)之后的所有内容都替换掉的正确方法是什么?
这是由正则表达式处理的。以下内容替换了从 rel="" 到字符串末尾的所有内容。
$newstring = preg_replace('/ rel="".*$/','',$oldstring);
.* 表示 "everything",$ 表示 "end of string"。我在 rel 之前添加了一个 space,因为我假设您也想删除它。
听起来您真正要查找的是当前字符串的 子字符串 ,直到 rel
属性的末尾。
$newstring = substr($oldstring, 0, strpos($oldstring, 'rel=""'));
使用preg_match:
preg_match("/(\<a href\=\".*\")\srel/", "<a href="http://example.com/myPDF.pdf" rel="">myPDF12345431234</a>", $output_array);
Var_dump($output_array);