-1

I need to have Title, some text after comma and the Title needs to be bold. I was thinking the way to do that would be to add <strong> tag at the beginning and somehow add a closing </strong> tag right before the comma.

I sort of found something that could do that: Insert string at specified position but it wasn't really working for me, maybe I did something wrong?

$newstr = substr_replace($oldstr, '</strong>', substr($oldstr, ','), 0); does not work for me. Any ideas on what's wrong there?

Edit: Why did I get a downvote?

Community
  • 1
  • 1
Xeen
  • 6,955
  • 16
  • 60
  • 111
  • This Looks like HTML code. So you should use a html Parser. – Jens Mar 16 '16 at 08:16
  • It wasn't me who downvoted. But I guess the reason would be that you didn't take a look at the official website of [PHP Substr()](http://php.net/substr). It would have told you why `substr` didn't work like that. It's just my guess though :) – icecub Mar 16 '16 at 08:33

3 Answers3

5

are you mean this ?

$str    = 'Hello World, i\'m here now';

$strEx  = explode(',', $str);

$title  = $strEx[0];

$text   = substr($str, strlen($title));

$strNew = '<strong>'.$title.'</strong>'.$text;

echo $strNew;
Ata
  • 312
  • 1
  • 7
1

The problem is your substr. It doesn't take a comma as a second parameter. Instead the second parameter should be an integer as the starting point to take part of the string.

An easier solution would be:

$oldstr = "Title, this is a test";
$search = "/,/";
$replace = "</strong>,";

$newstr = preg_replace($search, $replace, $oldstr);

echo $newstr;
icecub
  • 8,615
  • 6
  • 41
  • 70
1

if your case is like this always "Title, some text after comma", then you can replace the comma ',' with closing </strong>, and add <strong> at begin of string:

$str = "Title, some text after comma";
$result = "<strong>".str_replace(",","</strong>,",$str);
//<strong>Title</strong>, some text after comma

this way you will get what you need.

Yazan
  • 6,074
  • 1
  • 19
  • 33