Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

php search replace help

Options
  • 13-06-2006 7:56am
    #1
    Registered Users Posts: 648 ✭✭✭


    hi i have a load of text files that are marked up with wordpress like markup eg

    __ hello __ : underlines
    -- hello -- : is a h1 tag

    what i basically want to do is display these text files on a html page therby converting these foreign tags into the right html tags.

    my problem is that the __ tag for example is used for both the <u> and </u>... how do i dinguish between them.. i cant do a simple replace becuase of this

    (is there any way to say replace the first instance with <u> and the second with </u> and third with <u>)
    any other solutions??

    Tnx


Comments

  • Registered Users Posts: 8,488 ✭✭✭Goodshape


    Is there a PHP wild-card character you could use?

    ie. replace ____ %SOMETEXT% ____ with <u> %SOMETEXT% </u>, rather than just replacing ___.

    Not even sure if this is possible (just thinking out loud) but it seems like the sort of thing that could work.


  • Registered Users Posts: 648 ✭✭✭ChicoMendez


    Hmmm sounds interesting... but is it possible? ive looked around and cant find anything


  • Registered Users Posts: 6,511 ✭✭✭daymobrew


    hi i have a load of text files that are marked up with wordpress like markup
    Since wordpress is open source you should have a look to see how it renders stuff.
    Goodshape's suggestion sounds promising. Regular expressions are very powerful and can do amazing things, though creating the right regex can be a pain.


  • Registered Users Posts: 683 ✭✭✭Gosh


    (is there any way to say replace the first instance with <u> and the second with </u> and third with <u>)
    any other solutions??

    The following function will replace the first occurrence of ____ with <u>, the second with </u>, the third with <u> and so on.
    <?php
    
    function replace_chars($str){
        $on = true;
        $pos = strpos ($str, "____");
        while ($pos !== false) {
          if ($on) {  
             $on = false;
             $str = substr_replace($str, "<u> ",  $pos, 4);
          }else{
             $on = true;
             $str = substr_replace($str, "</u>", $pos, 4);
          }
           $pos = strpos ($str, "____");  
        }
        return $str;
    }
    
    ?>
    


  • Registered Users Posts: 6,511 ✭✭✭daymobrew


    Here is some ugly perl code that does the job (in my little experiment).
    [PHP]#!/usr/bin/perl -w

    use strict;

    my $string = 'Start ___ This stuff should be _underlined_ but not ___ stuff after wards. ___ A final bit ___ .';

    my $newstr;
    my $ul_start = 1;
    while ( $string =~ /(.*?) ___ /g )
    {
    # Append the bit before the tag.
    $newstr .= $1;
    # Add the appropriate start or end tag.
    if ( $ul_start )
    {
    $newstr .= ' *ULSTART*';
    $ul_start = 0;
    }
    else
    {
    $newstr .= '*ULEND* ';
    $ul_start = 1;
    }
    # If there are no more tags then store the rest of the string.
    if ( $string !~ / \G .*? ___/x )
    {
    $newstr .= substr( $string, pos($string) );
    }
    }
    print $newstr;[/PHP] prints
    Start *ULSTART*This stuff should be _underlined_ but not*ULEND* stuff after wards. *ULSTART*A final bit*ULEND* .
    

    I don't know if the PHP preg (Perl Compatible) functions support the '\G' extension though preg_replace_callback seems to have promise.

    Edit: The Perl \G assertion is not supported as it is not relevant to single pattern matches. (though a preg_match mentions it, possibly in unrelated usage).

    I did a bit of browsing through the WordPress code but didn't see anything obvious.
    The developer docs suggested that developers ask for help on the wp forums. You might get help there.


  • Advertisement
Advertisement