Enjoy an ad free experience by logging in. Not a member yet?
Register .
05-01-2010, 07:59 AM
PM User |
#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.
05-01-2010, 08:12 AM
PM User |
#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 ();
05-01-2010, 08:31 AM
PM User |
#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.
05-01-2010, 09:13 AM
PM User |
#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 ();
05-01-2010, 09:20 AM
PM User |
#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.
05-01-2010, 09:23 AM
PM User |
#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 ..
05-01-2010, 09:48 AM
PM User |
#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 ();
05-01-2010, 10:04 AM
PM User |
#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 ();
05-01-2010, 10:05 AM
PM User |
#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 ();
05-01-2010, 10:07 AM
PM User |
#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 ();
05-01-2010, 10:31 AM
PM User |
#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 ();
05-01-2010, 10:31 AM
PM User |
#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.
05-01-2010, 10:37 AM
PM User |
#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 ();
05-01-2010, 01:55 PM
PM User |
#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.
05-01-2010, 02:01 PM
PM User |
#15
Senior Coder
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
Post your existing code, as is.
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 09:53 AM .
Advertisement
Log in to turn off these ads.