如何删除字符串的一部分,即';用<;b></b>;,用php?
因此,我从db中提取了一条文本:如何删除字符串的一部分,即';用<;b></b>;,用php?,php,string,Php,String,因此,我从db中提取了一条文本: <b>Object title</b> <br /> <p>Object description</p> 对象标题 对象描述 我需要删除对象标题,我尝试使用substr,但没有找到正确的组合方法。你能告诉我怎么做吗 编辑 请注意:我只需要删除第一个元素,该字符串的每一个粗体部分都应该保持原样。这也可以在不使用正则表达式的情况下获得。 我们在这里使用简单的PHP字符串函数: 试试这个: <?p
<b>Object title</b>
<br />
<p>Object description</p>
对象标题
对象描述
我需要删除对象标题
,我尝试使用substr
,但没有找到正确的组合方法。你能告诉我怎么做吗
编辑
请注意:我只需要删除第一个
元素,该字符串的每一个粗体部分都应该保持原样。这也可以在不使用正则表达式的情况下获得。
我们在这里使用简单的PHP字符串函数:
试试这个:
<?php
function removeTags($subject, $replaceStr){
$strlength = strlen($replaceStr);
$subject = strstr($subject, $replaceStr, true) . substr(strstr($subject, $replaceStr), 0 + $strlength);
return $subject;
}
$str = '<b>Object title</b><b>Object title</b><br /><p>Object description</p><b>Object title</b>';
$brOpenRemoved = removeTags($str, '<b>');
$brCloseRemoved = removeTags($brOpenRemoved, '</b>');
echo $brCloseRemoved; // final output
?>
这也可以在不使用正则表达式的情况下获得。 我们在这里使用简单的PHP字符串函数: 试试这个:
<?php
function removeTags($subject, $replaceStr){
$strlength = strlen($replaceStr);
$subject = strstr($subject, $replaceStr, true) . substr(strstr($subject, $replaceStr), 0 + $strlength);
return $subject;
}
$str = '<b>Object title</b><b>Object title</b><br /><p>Object description</p><b>Object title</b>';
$brOpenRemoved = removeTags($str, '<b>');
$brCloseRemoved = removeTags($brOpenRemoved, '</b>');
echo $brCloseRemoved; // final output
?>
对于此问题,应使用preg\u match
$text = "<b>Object title</b><br /><p>Object description</p>";
if (!preg_match("/<b>.*<\/b>/",$text,$m)) {
str_replace($m[0],"",$text);
}
$text=“对象标题
对象描述”;
如果(!preg_match(“/.*/”,$text,$m)){
str_replace($m[0],“”,$text);
}
这应该适用于美国。
如果你想删除所有,你可以在$m上运行一个循环。你应该使用preg\u match来解决这个问题
$text = "<b>Object title</b><br /><p>Object description</p>";
if (!preg_match("/<b>.*<\/b>/",$text,$m)) {
str_replace($m[0],"",$text);
}
$text=“对象标题
对象描述”;
如果(!preg_match(“/.*/”,$text,$m)){
str_replace($m[0],“”,$text);
}
这应该适用于美国。
如果你想删除所有,你可以在$m上运行一个循环。如果你确定文本中有你的模式:
//remove first <b> tag
$text = '<b>foo</b>...';
$pos = strpos($text,'<b>');
if ($pos !== false) {
$newtext = substr_replace($text,'',$pos,strlen('<b>'));
}
//remove first </b> tag
$pos = strpos($text,'</b>');
if ($pos !== false) {
$newtext = substr_replace($newtext,'',$pos,strlen('</b>'));
}
//删除第一个标记
$text='foo…';
$pos=strpos($text,”);
如果($pos!==false){
$newtext=substr_replace($text,,$pos,strlen(“”));
}
//删除第一个标签
$pos=strpos($text,”);
如果($pos!==false){
$newtext=substr_replace($newtext,,$pos,strlen(“”));
}
如果您确定文本中有您的模式:
//remove first <b> tag
$text = '<b>foo</b>...';
$pos = strpos($text,'<b>');
if ($pos !== false) {
$newtext = substr_replace($text,'',$pos,strlen('<b>'));
}
//remove first </b> tag
$pos = strpos($text,'</b>');
if ($pos !== false) {
$newtext = substr_replace($newtext,'',$pos,strlen('</b>'));
}
//删除第一个标记
$text='foo…';
$pos=strpos($text,”);
如果($pos!==false){
$newtext=substr_replace($text,,$pos,strlen(“”));
}
//删除第一个标签
$pos=strpos($text,”);
如果($pos!==false){
$newtext=substr_replace($newtext,,$pos,strlen(“”));
}
您可以使用$limit
对preg\u replace
$yourString =
"<b>Object title</b>
<br />
<p>Object description</p>";
$limit = 1;
$pattern = "/<b>(.*)<\/b>/";
$replace = "";
$newString = preg_replace($pattern, $replace, $yourString, $limit);
echo $newString;
输出:
<br />
<p>Object description</p>
Object title
<br />
<p>Object description</p>
<b></b>
<br />
<p>Object description</p>
对象标题
对象描述
或者只保留不包含内容的标签:
$limit = 1;
$pattern = "/<b>(.*)<\/b>/";
$replace = "<b></b>";
$newString = preg_replace($pattern, $replace, $yourString, $limit);
echo $newString;
$limit=1;
$pattern=“/(.*)/”;
$replace=“”;
$newString=preg_replace($pattern,$replace,$yourString,$limit);
echo$newString;
输出:
<br />
<p>Object description</p>
Object title
<br />
<p>Object description</p>
<b></b>
<br />
<p>Object description</p>
对象描述
您可以使用$limit
对preg\u replace
$yourString =
"<b>Object title</b>
<br />
<p>Object description</p>";
$limit = 1;
$pattern = "/<b>(.*)<\/b>/";
$replace = "";
$newString = preg_replace($pattern, $replace, $yourString, $limit);
echo $newString;
输出:
<br />
<p>Object description</p>
Object title
<br />
<p>Object description</p>
<b></b>
<br />
<p>Object description</p>
对象标题
对象描述
或者只保留不包含内容的标签:
$limit = 1;
$pattern = "/<b>(.*)<\/b>/";
$replace = "<b></b>";
$newString = preg_replace($pattern, $replace, $yourString, $limit);
echo $newString;
$limit=1;
$pattern=“/(.*)/”;
$replace=“”;
$newString=preg_replace($pattern,$replace,$yourString,$limit);
echo$newString;
输出:
<br />
<p>Object description</p>
Object title
<br />
<p>Object description</p>
<b></b>
<br />
<p>Object description</p>
对象描述
你为什么不使用DOMDocument
@Uchiha没有使用过它,你能提供一个例子吗?:)我会用ereg_replace(是的,这需要你阅读并了解它是如何完成的)来做这件事。作为将来的参考:你没有删除第一个“”元素(没有这样的东西),但是你删除了第一个出现的文本“”和“”-如果是html,它们恰好是元素的开始和结束标记,但是,当您从数据库中提取文本时,php不知道或不关心我们可以如何处理它——它只是一个文本字符串。但这只是一个旁注;-)你为什么不使用DOMDocument
@Uchiha没有使用它,你能提供一个例子吗?:)我会用ereg_replace(是的,这需要你阅读并了解它是如何完成的)来做这件事。作为将来的参考:你没有删除第一个“”元素(没有这样的东西),但是你删除了第一个出现的文本“”和“”-如果是html,它们恰好是元素的开始和结束标记,但是,当您从数据库中提取文本时,php不知道或不关心我们可以如何处理它——它只是一个文本字符串。但这只是一个旁注;-)我想我更喜欢第一个函数——它更通用。我想我更喜欢第一个函数——它更通用。Regex不是解析HTML/xmlex的正确工具Regex不是解析HTML/XML的正确工具