CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Regex help. (http://www.codingforums.com/showthread.php?t=195132)

ShannenS 05-01-2010 07:59 AM

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.

Phil Jackson 05-01-2010 08:12 AM

what are you using to generate this code? is it php? if so, lets have a look!

ShannenS 05-01-2010 08:31 AM

At the moment it is static but it will be developed to php soon. At the moment I just need the regex statement thanks.

Phil Jackson 05-01-2010 09:13 AM

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

ShannenS 05-01-2010 09:20 AM

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.

ShannenS 05-01-2010 09:23 AM

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" 


Phil Jackson 05-01-2010 09:48 AM

give me 2 mins

Phil Jackson 05-01-2010 10:04 AM

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

Phil Jackson 05-01-2010 10:05 AM

p.s. a man once told me ANYTHING is possible and NOTHING is impossible with programming, you just don't know how yet.

Phil Jackson 05-01-2010 10:07 AM

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.

Phil Jackson 05-01-2010 10:31 AM

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>


ShannenS 05-01-2010 10:31 AM

Works when I place the list in the file but when I try to include it in a txt file it doesn't work.

Phil Jackson 05-01-2010 10:37 AM

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;
 
?>


ShannenS 05-01-2010 01:55 PM

Yes and all I'm getting is a blank page.
Having it directly in the page works though.

MattF 05-01-2010 02:01 PM

Post your existing code, as is.


All times are GMT +1. The time now is 10:22 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.