PHP';删除所有介于';特定词语
我有一份物品清单。每个项目都有其描述(每个项目的描述不同,但其结构保持不变),类似于:PHP';删除所有介于';特定词语,php,regex,replace,Php,Regex,Replace,我有一份物品清单。每个项目都有其描述(每个项目的描述不同,但其结构保持不变),类似于: [description] => Flat sandal <br />Blush<br />Laminated leather<br />Intertwining straps<br />Low heel: 0.5cm<br /> Product code: 5276870PS006703 <br /> Made
[description] => Flat sandal <br />Blush<br />Laminated leather<br />Intertwining straps<br />Low heel: 0.5cm<br />
Product code: 5276870PS006703 <br /> Made in: Italy<br />Composition: 100%Calfskin
[description]=>平底凉鞋
腮红
层压皮革
缠绕肩带
低跟:0.5cm
产品代码:5276870PS006703
意大利制造
成分:100%小牛皮
我需要删除每个项目描述的“产品代码:(随机数字和字母)”部分。我曾考虑过使用string_replace,但它只适用于替换单词“Product code”,而不适用于数字和字母,因为它们对于每个项目都是不同的。我还尝试:
$description = delete_all_between("Product code:", "<br />", $description);
$description=删除所有之间的内容(“产品代码:”,“
,$description”);
但它不起作用。
不知道我还能尝试什么
谢谢您可以修改以下代码:
$description = 'Flat sandal <br />Blush<br />Laminated leather<br />Intertwining straps<br />Low heel: 0.5cm<br />
Product code: 5276870PS006703 <br /> Made in: Italy<br />Composition: 100%Calfskin';
$pattern = '/Product code:\s*\w*\s*<br />/';
$replacement = '';
echo preg_replace($pattern, $replacement, $description);
解释:
$string = 'Flat sandal <br />Blush<br />Laminated leather<br/>Intertwining straps<br />Low heel: 0.5cm<br />Product code: 5276870PS006703 <br /> Made in: Italy<br />Composition:';
$pattern = '/Product code: (w+) /i';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
是一个php函数,它将在输入字符串中替换正则表达式定义的特定模式
使用的正则表达式产品代码:\s*\w*\s*
将匹配以产品代码开头的字符串:
,后跟一些空格字符,后跟一些单词字符,在以html
()结尾之前还有更多空格字符 使用preg_replace()函数
正则表达式
look for "product"
followed by \s+ (one or more spaces, tabs,...)
followed by "code"
followed by [^>]* (an unspecified amount of charakters that are not ">")
followed by \> an ">" (\ is es for escaping)
旗
你需要看看,它使用正则表达式,给你很大的力量去瞄准你想要的东西
类似于
$string = 'Flat sandal <br />Blush<br />Laminated leather<br/>Intertwining straps<br />Low heel: 0.5cm<br />Product code: 5276870PS006703 <br /> Made in: Italy<br />Composition:';
$pattern = '/Product code: (w+) /i';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
$string='平底凉鞋
腮红
层压皮革
缠绕带
低跟:0.5cm
产品代码:5276870PS006703
意大利制造
成分:';
$pattern='/产品代码:(w+)/i';
$replacement='';
echo preg_replace($pattern,$replacement,$string);
希望这有助于使用正则表达式替换。
i = ignore upper/lowercase
s = search multiple lines
$string = 'Flat sandal <br />Blush<br />Laminated leather<br/>Intertwining straps<br />Low heel: 0.5cm<br />Product code: 5276870PS006703 <br /> Made in: Italy<br />Composition:';
$pattern = '/Product code: (w+) /i';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);