Preg_match and Preg_replace trouble regular expressions Please HELP!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • stoppal@hotmail.com

    Preg_match and Preg_replace trouble regular expressions Please HELP!

    need to extract all text between the following strings, but not
    include the strings.


    "<!-- #BeginEditable "Title name" -->"
    "<p align="center"> #### </p>"


    I am using preg_match(???? , $s, $results)


    but I have had no success. I tried items like.. "<!-- #BeginEditable
    "Title name" -->".*."<p align="center"> #### </p>"


    But have had no luck.


    Please any assistenc is appreciated.


    In addition, I need to use preg_replace to replace the following:


    <a href="/folder2/folder1/folder/­page.html">


    with


    <a href="site.com= page.php?option ­=paper&itemid= page.html">


    Again, any help is appreciated. The regular expressions confuse me, my

    brain is about to explode from all the trouble shooting, I guess I'm
    just not smart emough to figure it out.


    PLEASE HELP!!


    thanks

  • Will Woodhull

    #2
    Re: Preg_match and Preg_replace trouble regular expressions Please HELP!

    stoppal@hotmail .com wrote:[color=blue]
    > need to extract all text between the following strings, but not
    > include the strings.
    >
    >
    > "<!-- #BeginEditable "Title name" -->"
    > "<p align="center"> #### </p>"
    >
    >
    > I am using preg_match(???? , $s, $results)
    >
    >
    > but I have had no success. I tried items like.. "<!-- #BeginEditable
    > "Title name" -->".*."<p align="center"> #### </p>"[/color]


    Try something like '/Title name" -->(.*)<p align="center/' for the
    regex, and look for the answer in $results[1].

    When you attempt to learn regexes, empty your mind of everything you
    know from PHP or other computer languages. The parens, dot, star, and
    other special characters are used in completely different ways than in
    procedural languages.

    Here's a quick link to PHP man:


    Here's a toot that others have found useful:

    (myself, I learned regex back in the day of awk, when all we had was
    woodburning computers)

    <snip>
    [color=blue]
    > In addition, I need to use preg_replace to replace the following:[/color]

    <snip>

    Well, you'll probably want something along the lines of

    $newString = preg_replace('/OldStuff/', "NewStuff", $oldString);

    but check out a tutorial or two to get a handle on all this 'stuff'.

    Friedl's _Mastering Regular Expressions_ is a good book on the subject.
    It is not a light read. But then regular expressions are really a
    computer language of their own, embedded into languages like PHP and
    Perl, but with their own separate syntax and rules.
    [color=blue]
    > The regular expressions confuse me, my
    > brain is about to explode from all the trouble shooting[/color]


    Yeah, been there. Wish you luck.

    Comment

    • stoppal@hotmail.com

      #3
      Re: Preg_match and Preg_replace trouble regular expressions Please HELP!

      Thank you for the responses, I'm going to try this stuff tonight and I
      hope it works.

      thanks

      Comment

      • cameron7

        #4
        Re: Preg_match and Preg_replace trouble regular expressions Please HELP!

        Regular Expressions are great. Less than 10 lines of code can be so
        powerful when you start to grasp the technology.
        =============== =============
        <?
        $fp=fopen("preg test.txt","r+") ;
        $content=fread( $fp,filesize("p regtest.txt"));
        fclose($fp);

        echo "\n\nBEFORE :\n------------------\n";
        echo $content;

        preg_match_all( "|<a href=\".+\">|m" ,$content,$matc hes);

        foreach($matche s[0] as $value)
        {
        preg_match("|\" .+/([^/]+)\">$|",$value ,$page);
        $pattern="(.+<!-- #BeginEditable \"Title name\" -->.+)" . $value .
        "(.+<p align=\"center\ ">#### </p>.+)";
        $replacement="< a href=\"site.com =page.php?optio n­=paper&itemid =" .
        $page[1] . "\">";
        $content=preg_r eplace("|$patte rn|s","\${1}" . $replacement .
        "\${2}",$conten t);

        }

        echo "\n\nAFTER: \n------------------\n";
        echo $content . "\n\n";
        ?>

        HERE IS THE OUTPUT:
        -------------------------------------------------

        BEFORE:
        ------------------
        some content that I don't want to edit...
        <a href="/folder9/folder8/folder7/-page555.html">
        <!-- #BeginEditable "Title name" -->
        <a href="/folder2/folder1/folder/­page.html">
        <a href="/folder9/folder8/folder7/-page555.html">
        <p align="center"> #### </p>
        More content that I don't want to edit...
        <a href="/folder2/folder1/folder/­page.html">


        AFTER:
        ------------------
        some content that I don't want to edit...
        <a href="/folder9/folder8/folder7/-page555.html">
        <!-- #BeginEditable "Title name" -->
        <a href="site.com= page.php?option ­=paper&itemid= ­page.html">
        <a href="site.com= page.php?option ­=paper&itemid=-page555.html">
        <p align="center"> #### </p>
        More content that I don't want to edit...
        <a href="/folder2/folder1/folder/­page.html">

        Comment

        • stoppal@hotmail.com

          #5
          Re: Preg_match and Preg_replace trouble regular expressions Please HELP!

          cameron7,

          Thank you for your post. But I'm still having trouble. I think your
          complicating what I'm trying to do. When I tried your code, nothing
          appeared on the page. I assume, there is a syntax error, but I may be
          wrong. I've tried debugging, with no luck. You included a loop
          through for each apearance of the pattern, but in my pages it only
          occurs once. Basically I'm removing an old header and footer, and
          replacing old links with a new path from the existing page. I
          appreciate you helping me.

          thanks

          Comment

          • stoppal@hotmail.com

            #6
            Re: Preg_match and Preg_replace trouble regular expressions Please HELP!

            Ok, I am getting closer but still no solution. I figured out your code
            had extra spaces, most likely a google thing, but I removed them and
            now I get the before part but the after section is blank. Any ideas?

            thanks

            Comment

            • stoppal@hotmail.com

              #7
              Re: Preg_match and Preg_replace trouble regular expressions Please HELP!

              Ok, sorry for all the extra posts, but I wanted to fill you in on where
              I am.

              When I use....

              $fp=fopen("http ://www.site.com/pregtest.html", "r+") ;
              $content=fread( $fp,filesize("h ttp://www.site.com/pregtest.html") );
              fclose($fp);

              etc. etc.

              I get .....

              "Before---------------After----------------", but that is it, no other
              content.


              If I change it to ...
              $content=implod e('', file("http://www.site.com/pregtest.html") );
              etc.etc..

              I get .....

              "Before---------------------
              ....ALL NORMAL CONTENT......"

              But then nothing under After, nor do I see the "AFTER"

              I am so lost I want to bang my head against a wall, I don't know much
              programming and this stuff is way over my head.

              thanks, any assistence is apprecaited.

              Comment

              • cameron7

                #8
                Re: Preg_match and Preg_replace trouble regular expressions Please HELP!

                <?

                error_reporting (E_ALL);

                /* INSERT CODE HERE */

                ?>

                Comment

                • Tim Van Wassenhove

                  #9
                  Re: Preg_match and Preg_replace trouble regular expressions Please HELP!

                  On 2005-08-06, cameron7 <cameron7@gmail .com> wrote:[color=blue]
                  ><?
                  >
                  > error_reporting (E_ALL);[/color]

                  // i find it easier to remember two calls to ini_set
                  //ini_set('error_ reporting', E_ALL);

                  // make sure they are displayed
                  ini_set('displa y_errors', TRUE);

                  --
                  Met vriendelijke groeten,
                  Tim Van Wassenhove <http://timvw.madoka.be >

                  Comment

                  • stoppal@hotmail.com

                    #10
                    Re: Preg_match and Preg_replace trouble regular expressions Please HELP!

                    I added the

                    // i find it easier to remember two calls to ini_set
                    //ini_set('error_ reporting', E_ALL);
                    // make sure they are displayed
                    ini_set('displa y_errors', TRUE);

                    line and when I use....

                    $fp=fopen("http ://www.site.com/pregtest.h tml","r+") ;
                    $content=fread( $fp,filesize("h ttp://www.site.com/pregtest.h tml"));
                    fclose($fp);

                    etc. etc.

                    I get .....


                    "Warning: fopen() expects at least 2 parameters, 1 given in
                    /Users/imac/Sites/folder/test.php on line 8

                    Warning: filesize(): Stat failed for http://www.site.com/test.php
                    (errno=2 - No such file or directory) in
                    /Users/imac/Sites/folder/test.php on line 9

                    Warning: fread(): supplied argument is not a valid stream resource in
                    /Users/imac/Sites/folder/test.php on line 9

                    Warning: fclose(): supplied argument is not a valid stream resource in
                    /Users/imac/Sites/folder/test.php on line 10
                    BEFORE: ------------------ AFTER: ----------------- -"

                    But, If I change it to ...
                    $content=implod e('', file("http://www.site.com/pregtest.h tml"));
                    etc.etc..

                    I get .....

                    "Before---------------------
                    ....ALL NORMAL CONTENT......"

                    Fatal error: Maximum execution time of 30 seconds exceeded in
                    /Users/imac/Sites/Folder/test.php on line 31

                    Comment

                    • stoppal@hotmail.com

                      #11
                      Re: Preg_match and Preg_replace trouble regular expressions Please HELP!

                      I was reviewing the code again, and correct me if I'm wrong, from what
                      I understand the current code only replaces.....

                      <a href="/folder2/folder1/folder/ page.html">

                      with

                      <a href="site.com= page.php?option =paper&itemid=p age.html">

                      if it's between each occurance of the preg_match.

                      I was trying to do two totally seperate items.

                      1) find one distinct pregmatch on a single page

                      2) replace links within a page

                      But as stated, they are totally independent.

                      If this simplifies matters, I can still use the help.

                      thanks again.

                      I've been working on this all morning, but am not having any luck.

                      thanks

                      Comment

                      • stoppal@hotmail.com

                        #12
                        Re: Preg_match and Preg_replace trouble regular expressions Please HELP!

                        I decided to focus on one item at a time. This is what I have so far,
                        but I get the following error...

                        Warning: Unknown modifier '(' in /Users/imac/Sites/folder/test.php on
                        line 17

                        when using the following code....

                        <?php
                        // i find it easier to remember two calls to ini_set
                        //ini_set('error_ reporting', E_ALL);
                        // make sure they are displayed
                        ini_set('displa y_errors', TRUE);

                        //replace "news.htm" with your file
                        $s=implode('', file("http://www.site.com/test.php"));

                        $pattern="<!-- #BeginEditable \"Title of Firmrecall\" -->(.*?)<p
                        align=\"center\ ">#### </p>";


                        if(preg_match($ pattern, $s, $results)){

                        echo $results[0];

                        }
                        ?>

                        Comment

                        • stoppal@hotmail.com

                          #13
                          Re: Preg_match and Preg_replace trouble regular expressions Please HELP!

                          this is line 17

                          $pattern="<!-- #BeginEditable \"Title of Firmrecall\" -->(.*?)<p
                          align=\"center\ ">#### </p>";

                          Comment

                          • stoppal@hotmail.com

                            #14
                            Re: Preg_match and Preg_replace trouble regular expressions Please HELP!

                            I'm begging for someone to help me out here. I've been working at this
                            non stop for the last for days, and I and not find a solution to my
                            problem.

                            This is the pattern I'm using to find everything between...

                            "<!-- #BeginEditable "Title of name" --> " AND "<p
                            align="center"> #### </p>"


                            $pattern="/<\!-- #BeginEditable \"Title of Firmrecall\" -->.*<p
                            align=\"center\ ">#### </p>/i";

                            But I get nothing.

                            PLEASE HELP!!!!

                            Comment

                            • John Dunlop

                              #15
                              Re: Preg_match and Preg_replace trouble regular expressions Please HELP!

                              Somebody wrote:
                              [color=blue]
                              > $pattern="/<\!-- #BeginEditable \"Title of Firmrecall\" -->.*<p
                              > align=\"center\ ">#### </p>/i";[/color]

                              a) Encode/escape the slash in your pattern; e.g., /<\/p>/.

                              b) Use different delimiters; e.g., `...`.

                              [Just probing, but why have you set the i modifier?]

                              --
                              Jock

                              Comment

                              Working...