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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Regex help.

  1. #1
    New Coder
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Regex help.

    I don't know if this is the right place to post this.

    Ok I've got this code
    PHP Code:
      <li><a name="symbols">(blah)</a></li>
      <
    li>123blah</li>
      <
    li>123</li>
      <
    li>123 blah</li>
      <
    li>blah 123</li>
      <
    li>blah123</li>
      <
    li><a name="a">A blah</a></li>
      <
    li>ablah</li>
      <
    li>a123blah</li>
      <
    li>ablah123</li
    and so on...

    As you can see the "a name" is related the first character of the string.
    I'm using a regex search and replace function and I need to be able to change this:
    PHP Code:
      <li><a name="a">A blah</a></li>
      <
    li>ablah</li
    into this:
    PHP Code:
      <li><a href="http://www.google.com.au/search?q=A+blah" name="a">A blah</a></li>
      <
    li><a href="http://www.google.com.au/search?q=ablah">ablah</li
    and so on...

    Can you guys figure out how to do this as regex is a very weak point for me.

  • #2
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    what are you using to generate this code? is it php? if so, lets have a look!
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #3
    New Coder
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts
    At the moment it is static but it will be developed to php soon. At the moment I just need the regex statement thanks.

  • #4
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    what i mean is how is the 'a' getting to be there?? is it dynamic, from a database, piece of code/script? do you own the site? If 'static' can't you just remove it? Why do you need regex?

    Regards, Phil
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #5
    New Coder
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I placed the "a" there. Problem is there is over 3000 lines. I can't manually append <a href="whatever"> to the start and </a> to the end. Remeber also that "whatever" has to equal the string inside the <li> tags.

  • #6
    New Coder
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts
    If my request seems impossible I can remove all tags and just have whatever is inside the <li> tags so instead of:
    PHP Code:
    <li>whatever</li>
    <
    li>whatever2</li>
    <
    li>2whatever</li>
    <
    li>2 whatever</li>
    <
    li>whatever 2</li
    it will just be:
    PHP Code:
    whatever
    whatever2
    2whatever
    2 whatever
    whatever 2 
    Then I just need a regex to turn
    PHP Code:
    whatever
    whatever2
    2whatever
    2 whatever
    whatever 2 
    into
    PHP Code:
    "whatever",
    "whatever2",
    "2whatever",
    "2 whatever",
    "whatever 2" 
    Last edited by ShannenS; 05-01-2010 at 09:32 AM.

  • #7
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    give me 2 mins
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #8
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    <?php

    #$file = "./dir-to-file.php";
    #$srt = file_get_contents($file);

    $str '<li><a name="symbols">(blah)</a></li> 
      <li>123blah</li> 
      <li>123</li> 
      <li>123 blah</li> 
      <li>blah 123</li> 
      <li>blah123</li> 
      <li><a name="a">A blah</a></li> 
      <li>ablah</li> 
      <li>a123blah</li> 
      <li>ablah123</li>  '
    ;
      
     
     
    $str preg_replace(
                         
    "#<li><a\s+name=\"(?:[^\"]+)\"\s?>([^<]+)</a></li>#is"
                         
    "<li><a href=\"http://www.google.com.au/search?q=$1\">$1</a><li>"
                         
    $str);
     
    $str preg_replace(
                         
    "#<li>([^<]+)</li>#is"
                         
    "<li><a href=\"http://www.google.com.au/search?q=$1\">$1</a><li>"
                         
    $str);
     
     echo 
    $str;

    ?>
    Code:
    <li><a href="http://www.google.com.au/search?q=(blah)">(blah)</a><li> 
      <li><a href="http://www.google.com.au/search?q=123blah">123blah</a><li> 
      <li><a href="http://www.google.com.au/search?q=123">123</a><li> 
      <li><a href="http://www.google.com.au/search?q=123 blah">123 blah</a><li> 
      <li><a href="http://www.google.com.au/search?q=blah 123">blah 123</a><li> 
      <li><a href="http://www.google.com.au/search?q=blah123">blah123</a><li> 
      <li><a href="http://www.google.com.au/search?q=A blah">A blah</a><li> 
      <li><a href="http://www.google.com.au/search?q=ablah">ablah</a><li> 
      <li><a href="http://www.google.com.au/search?q=a123blah">a123blah</a><li> 
      <li><a href="http://www.google.com.au/search?q=ablah123">ablah123</a><li>
    create a seperate file with the above code, load the code through and save the output
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #9
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    p.s. a man once told me ANYTHING is possible and NOTHING is impossible with programming, you just don't know how yet.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #10
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    also a better way would be not to use preg_replace but preg_match_all, the use the data to urlencode the data before sticking in the url.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #11
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    <?php

    #$file = "./dir-to-file.php";
    #$srt = file_get_contents($file);

    $str '<li><a name="symbols">(blah)</a></li> 
      <li>123blah</li> 
      <li>123</li> 
      <li>123 blah</li> 
      <li>blah 123</li> 
      <li>blah123</li> 
      <li><a name="a">A blah</a></li> 
      <li>ablah</li> 
      <li>a123blah</li> 
      <li>ablah123</li>  '
    ;
      
     
     
    $array preg_match_all("#<li><a\s+name=\"(?:[^\"]+)\"\s?>([^<]+)</a></li>#is"$str$matches);

     foreach( 
    $matches[1] as $key => $inner_text ) {
         
    $url_insert urlencode$inner_text );
         
    $str str_replace($matches[0][$key], "<li><a href=\"http://www.google.com.au/search?q=" $url_insert "\">" $inner_text "</a></li>"$str); 
     }
     
     
    $array preg_match_all("#<li>([^<]+)</li>#is"$str$matches); 
     
     foreach( 
    $matches[1] as $key => $inner_text ) {
         
    $url_insert urlencode$inner_text );
         
    $str str_replace($matches[0][$key], "<li><a href=\"http://www.google.com.au/search?q=" $url_insert "\">" $inner_text "</a></li>"$str); 
     }
     
     echo 
    $str;
     
    ?>

    Code:
    <li><a href="http://www.google.com.au/search?q=%28blah%29">(blah)</a></li> 
      <li><a href="http://www.google.com.au/search?q=123blah">123blah</a></li> 
      <li><a href="http://www.google.com.au/search?q=123">123</a></li> 
      <li><a href="http://www.google.com.au/search?q=123+blah">123 blah</a></li> 
      <li><a href="http://www.google.com.au/search?q=blah+123">blah 123</a></li> 
      <li><a href="http://www.google.com.au/search?q=blah123">blah123</a></li> 
      <li><a href="http://www.google.com.au/search?q=A+blah">A blah</a></li> 
      <li><a href="http://www.google.com.au/search?q=ablah">ablah</a></li> 
      <li><a href="http://www.google.com.au/search?q=a123blah">a123blah</a></li> 
      <li><a href="http://www.google.com.au/search?q=ablah123">ablah123</a></li>
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #12
    New Coder
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Works when I place the list in the file but when I try to include it in a txt file it doesn't work.

  • #13
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    are you trying:

    PHP Code:
    <?php

    $file 
    "./dir-to-file.php";
    $srt file_get_contents($file);
     
     
    $array preg_match_all("#<li><a\s+name=\"(?:[^\"]+)\"\s?>([^<]+)</a></li>#is"$str$matches);

     foreach( 
    $matches[1] as $key => $inner_text ) {
         
    $url_insert urlencode$inner_text );
         
    $str str_replace($matches[0][$key], "<li><a href=\"http://www.google.com.au/search?q=" $url_insert "\">" $inner_text "</a></li>"$str); 
     }
     
     
    $array preg_match_all("#<li>([^<]+)</li>#is"$str$matches); 
     
     foreach( 
    $matches[1] as $key => $inner_text ) {
         
    $url_insert urlencode$inner_text );
         
    $str str_replace($matches[0][$key], "<li><a href=\"http://www.google.com.au/search?q=" $url_insert "\">" $inner_text "</a></li>"$str); 
     }
     
     echo 
    $str;
     
    ?>
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #14
    New Coder
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yes and all I'm getting is a blank page.
    Having it directly in the page works though.

  • #15
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Post your existing code, as is.


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

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