简单:如何用 php 替换“所有之间”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6875913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple: How to replace "all between" with php?
提问by faq
$string = "<tag>i dont know what is here</tag>"
$string = str_replace("???", "<tag></tag>", $string);
echo $string; // <tag></tag>
So what code am i looking for?
那么我在寻找什么代码?
回答by Felix Kling
A generic function:
通用函数:
function replace_between($str, $needle_start, $needle_end, $replacement) {
$pos = strpos($str, $needle_start);
$start = $pos === false ? 0 : $pos + strlen($needle_start);
$pos = strpos($str, $needle_end, $start);
$end = $pos === false ? strlen($str) : $pos;
return substr_replace($str, $replacement, $start, $end - $start);
}
回答by rlemon
$search = "/[^<tag>](.*)[^<\/tag>]/";
$replace = "your new inner text";
$string = "<tag>i dont know what is here</tag>";
echo preg_replace($search,$replace,$string);
outputs:
输出:
<tag>your new inner text</tag>
回答by Fivell
$string = "<tag>I do not know what is here</tag>";
$new_text = 'I know now';
echo preg_replace('#(<tag.*?>).*?(</tag>)#', ''.$new_text.'' , $string); //<tag>I know now</tag>
回答by labue
If "tag" changes:
如果“标签”发生变化:
$string = "<tag>i dont know what is here</tag>";
$string = preg_replace('|^<([a-z]*).*|', '<></>', $string)
echo $string; // <tag></tag>
回答by anthonygore
If you don't know what's inside the <tag>
tag, it's possible there is another <tag>
tag in there e.g.
如果您不知道<tag>
标签内有什么,则可能还有另一个<tag>
标签,例如
<tag>something<tag>something else</tag></tag>
And so a generic string replace function won't do the job.
因此,通用字符串替换函数无法完成这项工作。
A more robust solution is to treat the string as XML and manipulate it with DOMDocument
. Admittedly this only works if the string is valid as XML, but I still think it's a better solution than a string replace.
更可靠的解决方案是将字符串视为 XML 并使用DOMDocument
. 诚然,这仅在字符串作为 XML 有效时才有效,但我仍然认为它是比字符串替换更好的解决方案。
$string = "<tag>i don't know what is here</tag>";
$replacement = "replacement";
$doc = new DOMDocument();
$doc->loadXML($str1);
$node = $doc->getElementsByTagName('tag')->item(0);
$newNode = $doc->createElement("tag", $replacement);
$node->parentNode->replaceChild($newNode, $node);
echo $str1 = $doc->saveHTML($node); //output: <tag>replacement</tag>
回答by Tomalak
$string = "<tag>i dont know what is here</tag>"
$string = "<tag></tag>";
echo $string; // <tag></tag>
or just?
要不就?
$string = str_replace($string, "<tag></tag>", $string);
Sorry, could not resist. Maybe you update your question with a few more details. ;)
对不起,忍不住了。也许你用更多细节更新你的问题。;)
回答by Caner
A generic and non-regex solution:
通用和非正则表达式解决方案:
I've modified @felix-kling's answer. Now it only replaces text if it finds the needles.
我修改了@felix-kling 的回答。现在它只在找到针时替换文本。
Also, I've added parameters for replacing the needles, starting position and replacing all the matches.
此外,我还添加了用于更换针头、起始位置和更换所有匹配项的参数。
I've used the mb_
functions for making the function multi-byte safe.
If you need a case insensitive solution then replace mb_strpos
calls with mb_stripos
.
我已经使用了mb_
使函数多字节安全的函数。如果您需要不区分大小写的解决方案,mb_strpos
请用mb_stripos
.
function replaceBetween($string, $needleStart, $needleEnd, $replacement,
$replaceNeedles = false, $startPos = 0, $replaceAll = false) {
$posStart = mb_strpos($string, $needleStart, $startPos);
if ($posStart === false) {
return $string;
}
$start = $posStart + ($replaceNeedles ? 0 : mb_strlen($needleStart));
$posEnd = mb_strpos($string, $needleEnd, $start);
if ($posEnd === false) {
return $string;
}
$length = $posEnd - $start + ($replaceNeedles ? mb_strlen($needleEnd) : 0);
$result = substr_replace($string, $replacement, $start, $length);
if ($replaceAll) {
$nextStartPos = $start + mb_strlen($replacement) + mb_strlen($needleEnd);
if ($nextStartPos >= mb_strlen($string)) {
return $result;
}
return replaceBetween($result, $needleStart, $needleEnd, $replacement, $replaceNeedles, $nextStartPos, true);
}
return $result;
}
$string = "{ Some} how it {is} here{";
echo replaceBetween($string, '{', '}', '(hey)', true, 0, true); // (hey) how it (hey) here{
回答by Nate S
If you need to replace the portion too then this function is helpful:
如果您也需要更换该部分,那么此功能很有帮助:
$var = "Nate";
$body = "Hey there {firstName} have you already completed your purchase?";
$newBody = replaceVariable($body,"{","}",$var);
echo $newBody;
function replaceVariable($body,$needleStart,$needleEnd,$replacement){
while(strpos($body,$needleStart){
$start = strpos($body,$needleStart);
$end = strpos($body,$needleEnd);
$body = substr_replace($body,$replacement,$start,$end-$start+1);
}
return $body;
}
I had to replace a variable put into a textarea that was submitted. So I replaced firstName with Nate (including the curly braces).
我不得不替换放入已提交的 textarea 中的变量。所以我用 Nate 替换了 firstName(包括花括号)。