PHP Put Span Class into Brackets regex -
this have far:
$phones = "samsung galaxy s8~lg g6~iphone 7 plus~ motorola z2"; $phones = str_replace("galaxy s", '<span class="galaxy">galaxy</span>', $phones); $value = preg_replace('/(\d+.*?)(~)/', '($1)$2', $phones . "~"); $value = "<li>". str_replace("~", ",</li><li>", substr($value,0,-1)) . "</li>"; echo $value;
and result is:
- samsung galaxy (8),
- lg g(6),
- iphone (7 plus),
- motorola z(2)
what i'm trying put span class inside brackets be:
- samsung ( galaxy 8 ),
- lg g(6),
- iphone (7 plus),
- motorola z(2)
thanks
code: (demo)
$phone='samsung galaxy s8~lg g6~iphone 7 plus~ motorola z2'; $match='/galaxy s(\d+)/'; $replace='( <span class="galaxy">galaxy</span> $1 )'; echo preg_replace($match,$replace,$phone);
unrendered output:
samsung ( <span class="galaxy">galaxy</span> 8 )~lg g6~iphone 7 plus~ motorola z2
here full <ul>
block:
$phone='samsung galaxy s8~lg g6~iphone 7 plus~ motorola z2'; $match='/galaxy s(\d+)/'; $replace='( <span class="galaxy">galaxy</span> $1 )'; echo '<ul><li>',str_replace('~',',</li><li>',preg_replace($match,$replace,$phone)),'</li></ul>';
unrendered output:
<ul><li>samsung ( <span class="galaxy">galaxy</span> 8 ),</li><li>lg g6,</li><li>iphone 7 plus,</li><li> motorola z2</li></ul>
final alteration:
$phones="samsung galaxy s8~lg g6~iphone 7 plus~ motorola z2"; $patterns=[ '/(?:galaxy s)?\d[^~]*/', // match (optional galaxy s), number, optional trailing text '/~ ?/', // match delimiter , optional trailing space (at motorola) '/galaxy s/' // literally match galaxy s ]; $replacements=[ '($0)', // wrap full string match in parentheses '</li><li>', // use closing , opening li tags new delimiter '<span class="galaxy">galaxy</span> ' // tagged text (note: g & space after </span>) ]; $full_list='<ul><li>'.preg_replace($patterns,$replacements,$phones).'</li></ul>'; echo $full_list;
unrendered output:
<ul><li>samsung (<span class="galaxy">galaxy</span> 8)</li><li>lg g(6)</li><li>iphone (7 plus)</li><li>motorola z(2)</li></ul>
Comments
Post a Comment