Results 1 to 6 of 6

Thread: Another Reg Ex Q

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default Another Reg Ex Q

    Is there a way using the preg_replace to find the same value from the first find. For example I think it'd be similiar to this

    Code:
    </(.*)>(.*)<$1>
    I'm trying to find the closing and opening of html tags. For example I have miscoded pages and I need to correct them

    Miscoded page looks like:
    Code:
    </li>
    Text in the wrong side<li>
    Thanks.
    Corrections to my coding/thoughts welcome.

  2. #2
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    This documentation should help you out.

    Basically, just replace that $1 with a \1.

  3. The Following User Says Thank You to techietim For This Useful Post:

    bluewalrus (01-22-2010)

  4. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Thanks. That works in dreamweaver but not on my page, any idea? I'm currently trying to get rid of misplaced line breaks.
    Code:
    $patterns[30] = '/<(.*)>(.*)<\/\1><br \/>/';
    $replacements[30] = '<$1>' . "$2" . '</$1>';
    Code:
    <li>This is an example.</li><br />
    To this:
    Code:
    <li>This is an example.<li>
    Corrections to my coding/thoughts welcome.

  5. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Is this what you are looking for?

    PHP Code:
    <?php
    $test
    ="<li>this is a test</li>
    <br>this should not be here.
    <li>good</li>"
    ;
    $test=preg_replace('/(<\/)(.*?)(>)(.*?)(<\2>)/s','$1$2$3$5',$test);
    echo 
    "$test";
    ?>
    It's sloppy, but should do the trick.
    To choose the lesser of two evils is still to choose evil. My personal site

  6. The Following User Says Thank You to james438 For This Useful Post:

    bluewalrus (02-15-2010)

  7. #5
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Got another one

    PHP Code:
    $patterns = array();
    $patterns[0] = '/<' "$browser'>(.*)' ."<\/$browser>" '/';
    $replacements = array();
    $replacements[0] = "<$browser>" '$1 + 1' "</$browser>";
    echo 
    preg_replace($patterns$replacements$stats); 
    How can I added 1 to the first value here?

    this currently just echos out the first value and + 1. Thanks.
    Corrections to my coding/thoughts welcome.

  8. #6
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    I am being lazy with my answer tonight. Partly because I have not worked much with arrays in PCRE code. The other reason is because I don't have all of the information like the string you are working with or what $browser is or $stats is.

    I think I can still help though. What you need to do here is use the e modifier. Take a look at the following example:

    PHP Code:
    <?php
    $text
    ="div1div2div3div4div5div6div7div";
    $text=preg_replace('/v(.*?)d/e',"v.($1+1).d",$text);
    echo
    "$text";
    ?>
    this will produce:

    div2div3div4div5div6div7div8div

    The e modifier will allow you to insert php into your regular expression whether it be a strtoupper function or str_replace function. The syntax involved is a little different though. Commands and captured substrings in the replacements are concatenated with a period. Captures are generally placed within single quotes or parentheses.

    I am a little unclear as to the exact syntax used when using the e modifier and php.net is not as clear on the matter as I would hope. In fact it is quite vague. If anyone wants to add to or correct what I have said in this post please do.
    Last edited by james438; 02-15-2010 at 12:53 PM.
    To choose the lesser of two evils is still to choose evil. My personal site

Bookmarks

Posting Permissions

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