regex - Remove words with specific ending from variable in PHP -
currently have code:
// main title of product $maintitle = 'chickenbuffet hot wings'; // take first word $maintitle , put in new variable list($title1) = explode(' ', $maintitle); // words start chicken removed , put in new variable $title2 = preg_replace('/(chicken)\w+/', '', $maintitle); // echo titles echo $title1; echo $title2;
this works fine, don't want remove words start chicken, words ending buffet. think has regex in preg_replace line, can't seem find correct expression.
thanks advance!
try regex:
#\w+buffet#
any word ending in buffet match.
<?php // main title of product $maintitle = 'chickenbuffet hot wings'; // take first word $maintitle , put in new variable list($title1) = explode(' ', $maintitle); // words start chicken removed , put in new variable $title2 = preg_replace('/\w+buffet/', '', $maintitle); // echo titles echo $title1."\n"; echo trim($title2);
which output :
chickenbuffet hot wings
try here: https://3v4l.org/fbmjk
Comments
Post a Comment