PHP 将跨度 Class 放入括号正则表达式
PHP Put Span Class into Brackets regex
这是我目前所拥有的:
$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+.*?)(~)/', '()', $phones . "~");
$value = "<li>". str_replace("~", ",</li><li>", substr($value,0,-1)) . "</li>";
Echo $value;
结果是:
- 三星 银河 (8),
- LG G(6),
- iPhone(7 加),
- 摩托罗拉 Z(2)
我想要做的是将跨度 class 放在括号内,这样它将是:
- 三星 ( galaxy 8 ),
- LG G(6),
- iPhone(7 加),
- 摩托罗拉 Z(2)
谢谢
代码:(Demo)
$phone='Samsung Galaxy S8~LG G6~iPhone 7 Plus~ Motorola Z2';
$match='/Galaxy S(\d+)/';
$replace='( <span class="galaxy">galaxy</span> )';
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> )';
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=[
'([=14=])', // 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>
这是我目前所拥有的:
$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+.*?)(~)/', '()', $phones . "~");
$value = "<li>". str_replace("~", ",</li><li>", substr($value,0,-1)) . "</li>";
Echo $value;
结果是:
- 三星 银河 (8),
- LG G(6),
- iPhone(7 加),
- 摩托罗拉 Z(2)
我想要做的是将跨度 class 放在括号内,这样它将是:
- 三星 ( galaxy 8 ),
- LG G(6),
- iPhone(7 加),
- 摩托罗拉 Z(2)
谢谢
代码:(Demo)
$phone='Samsung Galaxy S8~LG G6~iPhone 7 Plus~ Motorola Z2';
$match='/Galaxy S(\d+)/';
$replace='( <span class="galaxy">galaxy</span> )';
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> )';
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=[
'([=14=])', // 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>