Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with preg_match with regex 1

Status
Not open for further replies.

kaancho12

Technical User
Feb 22, 2005
191
hi,
i am trying to use php's preg_match to get elements inside certain HTML tags and i was wondering if anybody can help me figure this out.
I am trying to match all the links that have this pattern:
<A HREF="/ABC/specs/1thru10/ABC10.html">10</A>
When carrying out preg_match I am able to match them but is there a way to add more to the data "10" that's in between the HTML tags?????? I first have to refer to it but I cant seem to do that even.
here's my code:
if (preg_match("/(<A HREF=\/ABC\/specs\/[a-z][0-9][a-z]\/[a-z][0-9]\.[a-z]\/>)*?(<\/a>)/","<A HREF=\"/ABC/specs/1thru10/ABC10.html\">10</a>", $matches)){
echo $matches[0];
}
 
this looks like a two stage solution:

1. first find the data you want using regular expressions
2. work out what you want to replace using php
2. use preg_replace to insert the dynamic data.

further to your earlier post, the program pspad has a find and replace function that can help you build regular expressions (it's a kind of wizard).
 
hi,
thanks for your reply. but how am i able to "grab" the data that's between the <a href>"data"</a> tags. the code i have given above does successfully match the tags but i do not know how to grab the data that's between the tag.
thanks you again,
ko12
 
can you clarify whether you want the href string or the "data" string in your example above?
 
hi,
i want the "data" string. Actually i need to parse the data and look what value it has. Say, it has "20" then i need to create a button after </A> which will take the user to a different link other than that specified in <a Href>.
thanks again,
ko12
 
if (preg_match("/<A HREF=\/ABC\/specs\/[a-z][0-9][a-z]\/[a-z][0-9]\.[a-z]\/>(.*?)<\/a>/","<A HREF=\"/ABC/specs/1thru10/ABC10.html\">10</a>", $matches))

echo $matches[1];

Known is handfull, Unknown is worldfull
 
hi vbkris,
i could not make the above code work.. :((( i made some changes in it but still it did not seem to work.
if (preg_match("/<A HREF=\/ABC\/specs\/[0-9][a-z][0-9]\/[a-z][0-9]\.[a-z]\/>(.*?)<\/a>/","<A HREF=\"/ABC/specs/1thru10/ABC10.html\">10</a>", $matches)){
echo $matches[1];
}

is the code above right??
thanks
ko12
 
i'm really not great at regex but the code below populates an array called $matches with the "data" bits. the results are in a sub-array called $matches[1]. the code below grabs these and lets you create an array of the buttons you want to add

Code:
$filename ="";
$fh = fopen ($filename,"rb");
$contents = fread ($fh, filesize ($filename)); 
fclose($fh);
$regex = "'<[Aa][^>]*>(.*?)</[Aa]>'";
preg_match_all( $regex, $contents, $matches);
foreach($matches[1] as $key=>$val)
{
  switch($val)
  {
    case "abc32":
     $button[$val] = "<input type=\"button\" name=\"button_$val\" value=\"$val\" />"; //ie create an array with the found data as the key and the new button as the value.  nb assumes that there is only one instance of the found data per file.
     break;
    //etc etc
  }
}

once you have identified what you want to change i'd do something like:

Code:
//first read all the file into a string
foreach ($button as $key=>$val)
{ 
  str_replace ($key."<\a>", $key."<\a>".$val, $contents); //ie replace with itself plus the new button code
}

$newfilename = "parsed_".$filename;
$fh=fopen($newfilename,"wb");
fwrite($fh, $contents);
fclose ($fh);
 
hi jpadie,
thanks a lot :)) The first part of the code worked perfectly with what i wanted to do. i will try to second part and hopefully get done with this project soon :)))
ko12
 
glad to hear it

on the second bit of code, because you are using caps in your tags (ie <A> rather than <a>) is suggest you either change the str_replace line to use "<A>" or replace it with str_ireplace. the str_replace function is case sensitive.
 
jpadie,
another question:
instead of doing preg_match --if i use preg_replace shouldn't it work???
thanks
ko12
 
preg_replace replaces what you match with something else. you could use it provided:
1. you always knew what the replacement string would be (i.e. you didn't have to create it dynamically based on the value of "data"); and
2. you searched and replaced for the "data" AND the closing tag </a>. I am not sure how easy that would be.

also, regular expressions are slow (imho) and i would always use the core php functions like str_replace in place of regex unless i *really* needed the flexibility of the regex functions.
 
jpadie,
thank you for the tip. I used the regular expression that you have written and it works perfectly with what I have...many thank you
ko12
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top