Skip to content
Advertisement

PHP Bold String Before Specific Char

I have product title with this standard: “Brand name – item title” and I need to transform this string like this:

Brand name
item title

So I need 2 things to handle: replace the first dash in the string with “br” and apply bold to the text before the first dash.

I have reached only the first part of the goal with this code:

JavaScript

and this CSS:

JavaScript

But the result is not as expected because it “bold” only the first line, even if the text before the dash is on two lines..

Here a real title example:

POLO RALPH LAUREN – Camicia donna in Oxford azzurro

expected result:

POLO RALPH LAUREN
Camicia donna in Oxford azzurro

As you can see from the picture, when I’m on mobile view, “lauren” is on the second line and it’s not bold 🙁

–> I have found a javascript that suits the expected result:

JavaScript

BUT javascript is not fired anytime the layered navigation load new lines of products, so I think that using PHP would be the perfect solution, if possibile.

Advertisement

Answer

use this code instead:

JavaScript

remember to define bold class in your CSS file.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement