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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expressions

Status
Not open for further replies.

s0crates9

Technical User
Jun 18, 2005
70
US
Iam trying to isolate html tags, in particular link tags. I've been reading up on preg_match and regular expressions, however I am unsure of how to get a link pattern to be echoed to screen in the way I want it. For example, a typical link:
<a href="blah.com" class="myclass"><img src="image.jpg" alt="my image"></a>

How do I create a regular expression that will echo the entire link to screen as code and not as an actual link or linked image?

I appreciate feedback.
 
Just for clarity:
Regular expressions do not echo anything. They match patterns. Use the regex to extract the pattern and then echo it to the screen.

Suggestions:
Code:
$pattern = "/<a.*</a>/Uis";
preg_match_all($pattern, $subject, $matches);

Inspect the content of $matches and use htmlentities() when you output the match.
 
Hi DRJ478,

I am getting this error running the pattern above:
Code:
Warning: preg_match_all() [function.preg-match-all]: Unknown modifier 'a' in c:\documents and settings\test.test\Desktop\Testing\titlegrab.php on line 40

This is the code segment I am working with:
Code:
<?
function return_a($url)  {
		 $array = file ($url);
		  for ($i = 0; $i < count($array); $i++){
				if (preg_match_all("/<a.*</a>/Uis",$array[$i], $tag_contents)) {
				echo $atag = "&lt;".substr($tag_contents[0], 1, strlen($tag_contents[0]))."<br>";
				//echo"<pre>";print_r($tag_contents);echo"</pre>";
				//$atag = strip_tags($atag);
				}
		  }
$new_atag = return_a($url[$i]);
echo $new_atag;
?>


Thanks for any help on this!
 
I figured out that this pattern works, but not fully:
Code:
"/<a.*<\/a>/Uis"

but the output is as follows:

Code:
<a href="siteindex.asp" class="footerlinks">site index
<a href="services.asp" onMouseOver="imgAct('img1'); window.status='Services'; return true" onMouseOut="imgInact('img1'); window.status=''; return true">

And for some reason it does not place place image tag source, just empty picture boxes because the images are not available locally, and does not close with a trailing ?</a>".

Thanks!
 
I am still kind of in the dark as what you want to achieve.
Could we have an example of the HTML that is being processed and what the desired output is.
 
well I guess what I can do is use the code to explain further, there really is no "HTML" involved...
Here is what will run and give you my issue:
Code:
<?
$url=array("software is nice,<a href=blah.com target=_blank>See?</a>","What about the crazy hogs that took over? Did you see <a onMouseOver=\"blink()\" href=\"domain.com\">it</a>?");
function return_a($url){
          for ($i = 0; $i < count($url); $i++){
                if (preg_match_all("/<a.*<\/a>/Uis",$url[$i], $tag_contents)) {
                echo $atag = "&lt;".substr($tag_contents[0], 1, strlen($tag_contents[0]))."<br>";
                }
          }
        }
for ($i = 0; $i < count($url); $i++){
$new_atag = return_a($url[$i]);
echo $new_atag;
}
?>

I need something that will get link tags and spit them as well as any source or text within them such as: <a href="mysite.com"><img src="hle.jpg"></a> or <a target="parent" href="smoke.com">Text here</a>.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top