Re: PHP regular expression HELP

I need to make the first character of all words in a sentence different css styling. So I followed a code from the net:

<?php $title_uncut = get_the_title();
$title_cut = preg_replace('/(?:^|\b)(\p{L})(\p{L}*)/u', '<span class="cut_first">$1</span><span class="cut_second">$2</span>   ', $title_uncut);
echo '<div style="padding-left:30px">'.$title_cut.'</div>';

It works for normal wording but do not replace "&" correctly. You can see a sample here:

The "&" actually does appears, only it does not being captured to style properly.

Please help thanks!


  • Timothy Bowers
    • Chief Pigeon

    If you look at the html of the page:

    <div style="padding-left:30px">
    <span class="cut_first">P</span>
    <span class="cut_second">roducts</span>
    &nbsp;&nbsp;&nbsp; & 
    <span class="cut_first">S</span>
    <span class="cut_second">olutions</span>&nbsp;&nbsp;&nbsp;</div>    </div>

    Notice you have no span or css class/id around the ampersand?

    So no CSS is targeting it therefore its just controlled by the pages normal CSS.

    Take care.

  • Bucgene
    • Design Lord, Child of Thor

    Hi Timothy

    I'm sorry if I sound rude, but could you please help to read my question correctly before answering?

    I know the "&" are not being wrapped with the span, therefore controlled by normal css. If the "&" is being wrapped I would not be here, rite?

    That's why I'm asking for some help. The php regexp_replace do not take the "&" and wrap the <span> around. If anyone could help to modify the php statement a little to include & in the regexp?

    Really appreciated it.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.