目前我有这样的代码:
// main title of product
$maintitle = 'CHICKENBUFFET HOT WINGS';
// take first word from $maintitle and put in new variable
list($title1) = explode(' ', $maintitle);
// words that start with CHICKEN are removed and put in new variable
$title2 = preg_replace('/(CHICKEN)\w+/', '', $maintitle);
// echo titles
echo $title1;
echo $title2;这很好,但是我不想删除以鸡肉开头的单词,而是以自助餐结尾的单词。我认为这与preg_replace行中的REGEX有关,但我似乎找不到正确的表达式。
谢谢提前!
发布于 2017-09-12 08:38:33
试试这个正则表达式:
#\w+BUFFET#
任何以自助餐结尾的单词都会匹配。
<?php
// main title of product
$maintitle = 'CHICKENBUFFET HOT WINGS';
// take first word from $maintitle and put in new variable
list($title1) = explode(' ', $maintitle);
// words that start with CHICKEN are removed and put in new variable
$title2 = preg_replace('/\w+BUFFET/', '', $maintitle);
// echo titles
echo $title1."\n";
echo trim($title2);将产生以下结果:
CHICKENBUFFET
HOT WINGS在这里试试:https://3v4l.org/FbMjk
发布于 2017-09-12 08:35:55
当您需要字符串时,请用自助餐结束,请按以下方式进行更改
$title2 = preg_replace('/\w+(BUFFET)/', '', $maintitle);全码
$maintitle = 'CHICKENBUFFET HOT WINGS';
// take first word from $maintitle and put in new variable
list($title1) = explode(' ', $maintitle);
// words that start with CHICKEN are removed and put in new variable
$title2 = preg_replace('/\w+(BUFFET)/', '', $maintitle); // changed this line
// echo titles
echo $title1;
echo "<br/>";
echo $title2;https://stackoverflow.com/questions/46171545
复制相似问题