如何从 php 的句子中 select 第一个粗体字?
How to select first bold words from sentence by php?
我想 select 句子中的第一个粗体字。
如果我的句子是<b>My username here</b> Address: <b>Bangladesh.</b> Mobile: <b>xxxxxx</b>
这里我只想输出:My username here
我阅读了有关 preg_match 的内容,但无法理解如何应用它们。
只有第一个粗体部分然后使用下面的代码。
<?php
$text = '<b>My username here</b> Address: <b>Bangladesh.</b> Mobile: <b>xxxxxx</b>';
preg_match("'<b>(.*?)</b>'si", $text, $match);
echo $match[0];
?>
所有粗体搜索都使用这个
<?php
$text = '<b>My username here</b> Address: <b>Bangladesh.</b> Mobile: <b>xxxxxx</b>';
preg_match_all("'<b>(.*?)</b>'si", $text, $match);
foreach($match[1] as $val)
{
echo $val.' ';
}
?>
试试这个最简单的方法:
$text = "<b>My username here</b> Address: <b>Bangladesh.</b> Mobile: <b>xxxxxx</b>";
preg_match("/<b>(.*?)<\/b>/", $text, $match);
echo $match[0];
我想 select 句子中的第一个粗体字。
如果我的句子是<b>My username here</b> Address: <b>Bangladesh.</b> Mobile: <b>xxxxxx</b>
这里我只想输出:My username here
我阅读了有关 preg_match 的内容,但无法理解如何应用它们。
只有第一个粗体部分然后使用下面的代码。
<?php
$text = '<b>My username here</b> Address: <b>Bangladesh.</b> Mobile: <b>xxxxxx</b>';
preg_match("'<b>(.*?)</b>'si", $text, $match);
echo $match[0];
?>
所有粗体搜索都使用这个
<?php
$text = '<b>My username here</b> Address: <b>Bangladesh.</b> Mobile: <b>xxxxxx</b>';
preg_match_all("'<b>(.*?)</b>'si", $text, $match);
foreach($match[1] as $val)
{
echo $val.' ';
}
?>
试试这个最简单的方法:
$text = "<b>My username here</b> Address: <b>Bangladesh.</b> Mobile: <b>xxxxxx</b>";
preg_match("/<b>(.*?)<\/b>/", $text, $match);
echo $match[0];