0

I want to select first bold words from sentence.

If my sentence is <b>My username here</b> Address: <b>Bangladesh.</b> Mobile: <b>xxxxxx</b>

Here I want to output just: My username here

I read about preg_match, explode but cannot understand how to apply them.

Lemon Kazi
  • 3,308
  • 2
  • 37
  • 67
koc
  • 955
  • 8
  • 26

2 Answers2

0

For only first bold section then use below code.

<?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];
?>

for all bold search use this

<?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.' ';
}
?>

Here is the sample

Lemon Kazi
  • 3,308
  • 2
  • 37
  • 67
0

Try this simplest way :

$text = "<b>My username here</b> Address: <b>Bangladesh.</b> Mobile: <b>xxxxxx</b>";
preg_match("/<b>(.*?)<\\/b>/", $text, $match);

echo $match[0];
Root
  • 2,269
  • 5
  • 29
  • 58