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 5 of 5

Thread: preg_replace

  1. #1
    New Coder
    Join Date
    Sep 2007
    Location
    US
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 4 Posts

    preg_replace

    I'm having trouble with this and I don't see why.
    I visited countless numbers of manuals on this and they don't explain anything I need.

    PHP Code:
    $mytxt preg_replace('/<p style="text-align: center;">(.+?)<\/p>/is''[align=center]\\1[/align]'$mytxt); 
    I'm trying to get it to turn this bit of html into a custom bbcode, well the thing is whenever my text gets run through, I come out with absolutely nothing.
    No text nothing.
    I don't understand it, I've escaped what I needed to before this and still got the same results, so as a ditch effort I've done this in java and it looks exactly like this and still works fine, whats the problem with my PHP here?

    I know that < and > have meaning but if i add '\' that meaning is changed... and adding '\\' to it only escapes the first '\' making no difference at all...
    I really need some help here.

    Thanks any one who can help.
    I need to find a book about all this stuff. God, thats gonna be one big book!

    http://www.gamezftw.com
    Play On!

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    That code works fine:
    PHP Code:
    $mytxt = <<<END
    <p style="text-align: center;">test1</p>
    <p style="text-align: center;">test2</p>
    <p style="text-align: center;">test3</p>
    <p style="text-align: center;">test4</p>
    END;

    $mytxt preg_replace('/<p style="text-align: center;">(.+?)<\/p>/is''[align=center]\\1[/align]'$mytxt); 

    echo 
    $mytxt
    Outputs:
    Code:
    [align=center]test1[/align]
    [align=center]test2[/align]
    [align=center]test3[/align]
    [align=center]test4[/align]
    Output the code right before you use it to make sure you aren't modifying the code beforehand, and make sure it's being passed to preg_replace() how you're expecting it to.
    Last edited by Inigoesdr; 12-30-2007 at 03:43 AM.

  • #3
    New Coder
    Join Date
    Sep 2007
    Location
    US
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 4 Posts
    I really really don't understand this, perhaps I need some air...

    This or one of the others like it have to be the problem yet they are all the same.

    At any rate, thank you for your help, I will be sure to take your advice.
    I need to find a book about all this stuff. God, thats gonna be one big book!

    http://www.gamezftw.com
    Play On!

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Digicoder View Post
    I really really don't understand this, perhaps I need some air...

    This or one of the others like it have to be the problem yet they are all the same.

    At any rate, thank you for your help, I will be sure to take your advice.
    check to see in your code, not in this post, if spelling for $mytxt or whatever is same in both parts of the preg_replace

    more clear, the mistake could be:

    PHP Code:
    $othervar preg_replace(..., ..., $thisvar); 
    You can't see the difference if you test $thisvar, before and after preg_replace.

    best regards
    Last edited by oesxyl; 12-30-2007 at 04:13 AM.

  • #5
    New Coder
    Join Date
    Sep 2007
    Location
    US
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Thanks for the advice all.

    After some time away, I did locate the problem. I had used the wrong syntax for one of the regex's.

    A simple overlooked slash can make all the difference.
    I should break more often, working like this can't be too good for a person.

    thanks again.
    I need to find a book about all this stuff. God, thats gonna be one big book!

    http://www.gamezftw.com
    Play On!


  •  

    Posting Permissions

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