Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    replace <p>...</p> with <span>...</span>

    Hi,

    From db variable am getting content like this "<p>This is the description</p>"

    I need to replace it to "<span>This is the description</span>"

    I used "str_replace()" but this function is for 1 variable and i have to use 2 variable

    1st is for <p> and 2nd for </p>

    Please help


    Regards

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    str_replace can take array parameters:
    php.net/manual/en/function.str-replace.php

    Although you might be better with a regular expression, str_replacing with array parameters will work.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #3
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post
    You can just do it twice in a row

    PHP Code:

    $string 
    "<p>Hello</p>";
    $string str_replace("<p>""<span>"$string);
    $string str_replace("</p>""</span>"$string);
    echo 
    $string

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by Kekke View Post
    You can just do it twice in a row

    PHP Code:

    $string 
    "<p>Hello</p>";
    $string str_replace("<p>""<span>"$string);
    $string str_replace("</p>""</span>"$string);
    echo 
    $string
    Or, use arrays like I said
    PHP Code:
    $string '<p>hello</p>';
    $string str_replace(array('<p>','</p>'),array('<span>','</span>'),$string); 
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #5
    New Coder
    Join Date
    Sep 2011
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Hi,

    BluePanther, you are right but the situation is there is more then lines are retrieving like this

    i-e:
    <p>Hello 1</p>
    <p>Hello 2</p>
    <p>Hello 3</p>

    I need to change for first line only like this

    i-e
    <span>Hello 1</span>
    <p>Hello 2</p>
    <p>Hello 3</p>


    Please help


    Regards
    Last edited by shajiuddin; 01-16-2012 at 08:38 AM. Reason: correction

  • #6
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by shajiuddin View Post
    Hi,

    BluePanther, you are right but the situation is there is more then lines are retrieving like this

    i-e:
    <p>Hello 1</p>
    <p>Hello 2</p>
    <p>Hello 3</p>

    I need to change for first line only like this

    i-e
    <span>Hello 1</span>
    <p>Hello 2</p>
    <p>Hello 3</p>


    Please help


    Regards
    Is this first line always going to have the same content inside the tags?
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #7
    New Coder
    Join Date
    Sep 2011
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Hi,

    content will be same always but the tags will remain same.


    Regards

  • #8
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by shajiuddin View Post
    Hi,

    content will be same always but the tags will remain same.


    Regards
    If the content remains the same in the first tag, but the content in the other tags is different, why not just do this?

    PHP Code:
    $string str_replace('<p>Content</p>','<span>Content</span>',$string); 
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #9
    New Coder
    Join Date
    Sep 2011
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Sorry I had mistake in my wordings

    content will not be same always but the tags will remain same.

  • #10
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Ah, ok. In this case, a regular expression is probably your best bet:
    PHP Code:
    $string '<p>one</p><p>two</p><p>three</p>';
    $string preg_replace('/\<p>(.*?)\<\/p>/i','<span>${1}</span>',$string,1);
    // $string is now <span>one</span><p>two</p><p>three</p> 
    The preg_replace function takes a match parameter, a replacement paramenter, the subject, then an optional replacement limit and count. We supply the limit as 1, meaning it will only replace the first match.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •