Web Hosting Talk







View Full Version : Need help with php regular expressions


mrzippy
03-27-2006, 11:54 AM
Hello,

I'm stuck with this php regular expressions coding problem and could really use some help. :)

Let's say I have the following HTML/text:
<link HREF="http://localhost/test/test-styles1.css" TYPE="text/css" REL="stylesheet" >
<link HREF=test-styles1.css TYPE="tfext/css" REL="stylesheet">
<link HREF=test-styles1.css TYPE="tfext/css">
<link TYPE=tfext/css HREF=test-styles1.css >
<link TYPE=text/css HREF=test-styles1.css >
<link TYPE="text/css" HREF=test-styles1.css >
<link TYPE="texft/css" HREF=test-styles1text.css >
<LINK REL="stylesheet" HREF="/test/test-styles.css" TYPE="text/css">
<link HREF=test-styles1.css TYPE="text/cs3s" REL="stylesheet">

I am trying to use the preg_match_all() function to return an array that contains only the lines that have the <link and text/css text. (So for the example above, only line #1, 5,6 and 8 should be included in the "match" array.)

I have it so that it matches only the <link text, like this:

$matches = array();
preg_match_all("/<LINK(.*)>/iU", $text, $matches);
print_r($matches);exit;


But I don't know how to change it so that it matches only if both <link and text/css are in the string.

Any ideas?

:)

orbitz
03-27-2006, 12:56 PM
Yopu can try this:

preg_match_all('/(<link)(.*)(text\/css)/iU', $text, $matches);

azizny
03-27-2006, 01:52 PM
maybe:


preg_match_all('/(<link .* type="text\/css" .*)/iU', $text, $matches);


Peace,