4

I have a text string in PHP:

<strong> MOST </strong> of you may have a habit of wearing socks while sleeping. 
<strong> Wear socks while sleeping to prevent cracking feet</strong>
<strong> Socks helps to relieve sweaty feet</strong>

We can see, the first strong tag is

<strong> MOST </strong>

I want to remove the first strong tag, and make word inside of it to ucwords (first letter capitalized). Result like this

Most of you may have a habit of wearing socks while sleeping. 
<strong> Wear socks while sleeping to prevent cracking feet</strong>
<strong> Socks helps to relieve sweaty feet</strong>

I have tried with explode function, but it seem not like what I want. Here is my code

<?php
$text = "<strong>MOST</strong> of you may have a habit of wearing socks while sleeping. <strong> Wear socks while sleeping to prevent cracking feet</strong>. <strong> Socks helps to relieve sweaty feet</strong>";
$context = explode('</strong>',$text);
$context = ucwords(str_replace('<strong>','',strtolower($context[0]))).$context[1];
echo $context;
?>

My code only result

Most of you may have a habit of wearing socks while sleeping. <strong> Wear socks while sleeping to prevent cracking feet
Kerem
  • 11,377
  • 5
  • 59
  • 58
Riyanto Wibowo
  • 364
  • 1
  • 6
  • 13

5 Answers5

6

You can fix your code by using the optional limit argument of explode:

$context = explode("</strong>",$text,2);

However, it would be better as:

$context = preg_replace_callback("(<strong>(.*?)</strong>)",function($a) {return ucfirst($a[1]);},$text);
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • +1 For using the `preg_replace_callback` function. But it should be `return ucfirst(strtolower($a[1]));` because the first match 'MOST' is all caps, so you need to lower it first, then `ucfirst` it. Also, you need to pass in the 4th argument to `preg_replace_callback` - limit as 1, as the question only wants to remove the first `strong` tag. – Chris Feb 02 '13 at 05:09
3

I know you asked for a solution in PHP, but I don't think showing you a CSS solution would hurt:

HTML

<p><strong>Most</strong> of you may have a habit of wearing socks while sleeping.</p>

CSS

p strong:first-child {
    font-weight: normal;
    text-transform: uppercase;
}

Unless there's a specific reason for using PHP, I think it's simply complicating something that should be easy. Using CSS reduces server load and leaves styling where it should be.

UPDATE: Here's a fiddle.

thordarson
  • 5,943
  • 2
  • 17
  • 36
0

This would make sense:

preg_replace("<strong>(.*?)</strong>", "$1", 1)
Ruslan Osipov
  • 5,655
  • 4
  • 29
  • 44
0

This provides preg_replace_callback;

$s = '<strong> MOST </strong> of you may have a habit of wearing socks while sleeping.
      <strong> Wear socks while sleeping to prevent cracking feet</strong>
      <strong> Socks helps to relieve sweaty feet</strong>';
$s = preg_replace_callback('~<strong>(.*?)</strong>~i', function($m){
    return ucfirst(strtolower(trim($m[1])));
}, $s, 1);
print $s;

Out;

Most of you may have a habit of wearing socks while sleeping.
<strong> Wear socks while sleeping to prevent cracking feet</strong>
<strong> Socks helps to relieve sweaty feet</strong>
Kerem
  • 11,377
  • 5
  • 59
  • 58
0

I have to agree with @thordarson his answer doesn't change your content which to me is better. Because your issue is basically a layout issue. This my adapted version from his answer. The difference is that you first restate the strong text back to normal format. Then you capitalize the first letter.

   strong {
        font-weight: normal;
    }
    strong:first-letter {
        font-weight: normal;
        text-transform: uppercase;
    }

FIDDLE DEMO

Mr. Radical
  • 1,847
  • 1
  • 19
  • 29