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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jan 2004
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Replacing select text in strings?

    Is there a way I can have php sort though text, and replace any matching text blurbs, with another text blurb?

    Ie replace any [tagsLikeThis]

    With <this><this></and></this>

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Can do something like this
    PHP Code:
    <?php
    $str 
    "[tag]weeeeee[/tag]";

    $tags[0] = "/[tag]/";
    $tags[1] = "/[\/tag]/";

    $replaced[0] = "<b>";
    $replaced[1] = "<\/b>";

    echo 
    preg_replace($tags,$replaced,$str);
    ?>
    Last edited by Nightfire; 03-23-2004 at 10:46 AM.

  • #3
    Regular Coder
    Join Date
    Jan 2004
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by Nightfire
    Can do something like this
    PHP Code:
    <?php
    $str 
    "[tag]weeeeee[/tag]";

    $tags[0] = "/[tag]/";
    $tags[1] = "/[\/tag]/";

    $replaced[0] = "<b>";
    $replaced[1] = "<\/b>";

    echo 
    preg_replace($tags,$replaced,$str);
    ?>
    Thank you!

    So, if I ran an entire imported html doc through as $str, would it find and replace every [tag] like that?

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    it should do, but I've probably just put my foot in it now Too tired to think lol

  • #5
    Regular Coder
    Join Date
    Jan 2004
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by Raraken
    Thank you!

    So, if I ran an entire imported html doc through as $str, would it find and replace every [tag] like that?
    Thank you for the help!

  • #6
    Regular Coder
    Join Date
    Jan 2004
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay... Thers a problem.

    PHP Code:
    <?php 
    $str 
    = {include("document.htm")}; 

    $tags[0] = "/[tag]/"
    $tags[1] = "/[/tag]/"

    $replaced[0] = "<hr color=\"green\">"
    $replaced[1] = "<hr color=\"black\">"

    echo 
    preg_replace($tags,$replaced,$str[-1]);
    ?>
    That's my code.

    I'm trying to convert document.htm (The link works) into a string.

    But it says theres a syntax error on line 2 ($str = ... ... tm"]}, and when I take it away so its correct grammar, it say's that the function is wrong, and the reason is "t"

    Argh...

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    you can't assign an include like that (without output buffering etc)

    PHP Code:
    <?php  
    $str 
    file_get_contents("document.htm") ;  
    ?>
    or for older php versions
    <?
    $str 
    implode'' file("document.htm") ) ;
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #8
    Regular Coder
    Join Date
    Jan 2004
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In the script:

    PHP Code:
    <?php 
    $str 
    file_get_contents("document.htm"); 

    $tags[0] = "/[tag]/";  
    $tags[1] = "/[/tag]/"

    $replaced[0] = "_"
    $replaced[1] = "<hr color=\"black\">"

    echo 
    preg_replace($tags,$replaced,$str);
    ?>
    when it runs, I get this:

    Warning: Unknown modifier 't' in /home/sites/site5/web/hypertestfolder/hypercodetest.php on line 10
    <_i_le>Un_i_led Documen_ hyper_____op HELLO!

    I replaced the pervious HR codes in the first replacement array, with "_" codes. so I could more easily see what was happening.

    Basically, it went though and found the LETTERS in the tag section, instead of looking for the whole quote.

    So instead of finding "[tag]" it looks for: [, t, a, g, /, and ].

    Is there a way to correct this?
    Last edited by Raraken; 03-23-2004 at 09:24 PM.

  • #9
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Sorry, I should've checked. The forum's stripped my backslashes out my post.

    PHP Code:
    <?php 
    $str 
    file_get_contents("document.htm"); 

    $tags[0] = "/\\[tag]/";  
    $tags[1] = "/\\[\\/tag]/"

    $replaced[0] = "_"
    $replaced[1] = "<hr color=\"black\">"

    echo 
    preg_replace($tags,$replaced,$str);
    ?>
    That'll work now


  •  

    Posting Permissions

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