这是我到目前为止所做的:
2017-08-17T19:11:14.337+0530 E QUERY [thread1] SyntaxError: missing ; before statement @(shell):1:9
结果是:
我想要做的是将span类放在括号内,这样就可以了:
由于
答案 0 :(得分:1)
代码:(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);
未渲染的输出:
Samsung ( <span class="galaxy">galaxy</span> 8 )~LG G6~iPhone 7 Plus~ Motorola Z2
以下是完整的<ul>
块:
$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>';
未渲染的输出:
<ul><li>Samsung ( <span class="galaxy">galaxy</span> 8 ),</li><li>LG G6,</li><li>iPhone 7 Plus,</li><li> Motorola Z2</li></ul>
最后的改动:
$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 and optional trailing space (at Motorola)
'/Galaxy S/' // literally match Galaxy S
];
$replacements=[
'($0)', // wrap full string match in parentheses
'</li><li>', // use closing and opening li tags as 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;
未渲染的输出:
<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>