Preg_match - retrieving href - php

I have got stuck with a question I have just been helped on - its a new problem but only just slightly.
I have this preg_match to get the contents of href. Please don't tell me not to use regex - I am aware of using other parsers/classes etc but this is an old script that just needs to be fixed for now. :) No time for re-writes!
preg_match("~<a target=\'_blank\' rel=\'nofollow\' href=\"(.*?)\">~i", $epilink, $epiurl);
It returns:
http://www.example.com/frame2.php?view=&epi=54673-r
However, it should return:
http://www.example.com/frame2.php?view=168204&epi=54673
This is an example of html it would work with:
<a target='_blank' rel='nofollow' href="http://www.example.com/frame2.php?view=545903&epi=54683">
Why is the URL I have returned malformed?
Thanks all for any help.

$string="<a target='_blank' rel='nofollow' href=\"http://www.example.com/frame2.php?view=545903&epi=54683\">";
$s = explode('">',$string);
foreach($s as $k){
if (strpos($k,"href")!==FALSE){
echo preg_replace('/.*href="|/ms',"",$k);
break;
}
}
output
$ php test.php
http://www.example.com/frame2.php?view=545903&epi=54683

This should work:
$epilink = "<a target='_blank' rel='nofollow' href=\"http://www.example.com/frame2.php?view=545903&epi=54683\">";
preg_match("/<a target='_blank' rel='nofollow' href=\"(.*?)\">/i", $epilink, $epiurl);
print_r($epiurl);
you can also use preg_match_all

Related

Quotes problems inside echo

I want to put this line into echo :
<a title=\"Details\" href=\"produit.php?Id_Produit=".$row["id_produit"]."\"><img src=\"photos_produits/".$row["image_url"]."\" /></a>
How can I do this please, I have problem with quotes !
Thanks in advance.
Try this, it might work
echo "<a title='Details' href='produit.php?Id_Produit=" .$row["id_produit"]. "'"><img src='photos_produits".$row["image_url"]. "'/></a>"
Use the easiest, heredoc
$str = <<<ANCHOR
<a title="Details" href="produit.php?Id_Produit={$row["id_produit"]}"><img src="photos_produits/{$row["image_url"]}"></a>
ANCHOR; // make sure there is no extra spaces at the start of this line
echo $str;

php code inside a href tag not working

I would like to redirect to other pages but the = sign isn't working, the page name, the title, and the id are coming from database. I tried putting . before the = but that doesn't work. I think this should be simple but couldn't figure it out. can someone help me out?
echo "".click here."";
Try like
echo "<a href='".$row['page']."=".$row['topic']."&id=".$row['id']."'>click here</a>";
Like this ?
echo 'click here';
You were just missing a few quotes and dots. This will work.
echo "<a href='".$row['page']."=".$row['topic']."&id=".$row['id']."'>click here</a>";

IMG SRC Using Path from MySQL and PHP

I have the following line of code which doesn't seem to be working:
echo "<img src='/images/albumart'"; echo $row['art1']; echo "/>";
The 'art1' is definitely returning '/imagename.png' so the URL should be complete however nothing being displayed.
Any ideas?
Thanks.
You really should be concatenating that string:
<?php
echo '<img src="/images/albumart'.$row['art1'].'"/>';
?>
and ideally break out of php to do this:
<img src="/images/albumart<?php echo $row['art1']; ?>"/>
If you're not familiar, the period in php joins strings. That's called concatenation. No need to echo little bits like that, in fact please never do it the way you demonstrate. The root of your issue is, as Geoff pointed out, you weren't closing the src param.
echo "<img src='/images/albumart'"; echo $row['art1']; echo "/>";
It looks like you are closing the img src paramter after the albumart part.,
try this:
echo "<img src='/images/albumart"; echo $row['art1']; echo "' />";

How to echo html with variables in herf?

I tried to echo the following html, I can see from firebug that the variables are correctly printed, but it doesn't show up on the webpage. Would anyone please help me correct this?
$html ="<p><a rel='friends' href='$domain/$dir/$tag/$version/' target='_blank' $img></a></p>";
Your $img is in the wrong spot:
$html ="<p><a rel='friends' href='$domain/$dir/$tag/$version/' target='_blank'>$img</a></p>";
Then again I think you want to do this:
$html ="<p><a rel='friends' href='$domain/$dir/$tag/$version/' target='_blank'><img src='$img' /></a></p>";
There is nothing between the and the tags. you need something like this:
<p><a rel='friends' href='$domain/$dir/$tag/$version/' target='_blank'> $img </a></p>

calling function on hyperlink onclick event in php

can anybody help me here please?
I am using AJAX for pagination in my application. So I am generating hyperlinks with for loop. as follow:
for($t=1; $t<=$hf; $t++)
{
if($t == $_GET['pageno'])
{
echo $t." ";
}
else
{
echo "<a id ='$t' href='javascript:void(0)' onclick='open_page('ajaxinfo.php','content'); javascript:change('$t');'>$t</a>"." ";
}
}
Above echo statement does not give call to function. But instead of this when i just write html hyperlink it works fine and I get to see page2.html, my HTML code is:
<a id="page2" href="javascript:void(0)" onclick="open_page('ajaxinfo.php','content'); javascript:change('page2');">page2</a>
I don't understand why this is so? But is there any problem in echo's quotes.
Please Help.
that because you have syntax error while building anchors. Try to use double quotes for tag attributes and escape them with backslash.
So, your ECHO should look like this:
echo "<a id =\"{$t}\" href=\"javascript:void(0)\" onclick=\"open_page('ajaxinfo.php','content'); javascript:change('{$t}');\">{$t}</a> ";
You have to have code to add the contents returned by the ajax to the page. I don't see that anywhere.

Categories