What am I Doing wrong with str_replace ()? - php

Here is the HTML Code :
<a href="?loadurl=/search/Battlefield 3/1/99/0/">
<img src="static/img/next.gif" border="0" alt="Next" />
</a>
And this is the PHP Code :
//Fix Icons
$toremove = str_replace("next.gif\" border=\"0\" alt=\"Next\">", "dot.jpg\" border=\"0\" alt=\"Next\"><i class=\"icon-magnet\" style=\"color: #ffdd00;text-decoration: none;\"></i>", $toremove);
What am I doing wrong ?
Any help would be appreciated :)
~Kazilotus

Your HTML is using XHTML syntax: <img ... /> but your PHP is looking for HTML syntax: <img ... >. You need to make up your mind which to use and stick with it.
For example,
$toremove = str_replace("next.gif\" border=\"0\" alt=\"Next\">", "dot.jpg\" border=\"0\" alt=\"Next\"><i class=\"icon-magnet\" style=\"color: #ffdd00;text-decoration: none;\"></i>", $toremove);
Should be:
$toremove = str_replace("next.gif\" border=\"0\" alt=\"Next\" />", "dot.jpg\" border=\"0\" alt=\"Next\"><i class=\"icon-magnet\" style=\"color: #ffdd00;text-decoration: none;\"></i>", $toremove);
In your sample code.

Related

i want to pass php variable through the dynamic href link image to another php page

here i want to pass a dynamic variable '$cost' to another php page 'onewaytrip_passanger_data.php', through dynamically generated href link image. The code for href link image write into php tag
This is my code. but it is not working..
<?php
$cost=$row['fare'];
echo '<img src="images/bookbutton.png" width="85" height="20" />';
?>
Change this :
echo '<img src="images/bookbutton.png" width="85" height="20" />';
to
echo '<img src="images/bookbutton.png" width="85" height="20" />';
use this instead:
echo '<img src="images/bookbutton.png" width="85" height="20" />';
echo "<img src=\"images/bookbutton.png\" width="85" height="20" />";
or
echo "<img src=\"images/bookbutton.png\" width="85" height="20" />";
Always keep yourself confuse free. What is the need to do echo anchor tag. When, it can be done like this way.
Now, try.
<?php $cost=$row['fare'];?>
<a href="onewaytrip_passenger_data.php?fare=<?echo $cost;">
<img src="images/bookbutton.png" width="85" height="20" />
</a>

Getting the href attribute and text of certain kind of links

Of these four links:
<img border="0" src="imagenes/flech.gif" width="6" height="8">
Albano Y Romina Power<br>
<img border="0" src="imagenes/flech.gif" width="6" height="8">
Armando Manzanero<br>
<a name="inicio21" href="musica-Merengue-de-Banda_Cuisillos.html">
<img border="0" src="imagenes/flech.gif" width="6" height="8">Banda Cuisillos</a><br>
Baladas Alternativas<br>
I'm trying to capture the href value and the text of the link of the three first, leaving out the fourth link, in other words i'm trying to get this:
escuchar-baladas-de-Albano_Y_Romina_Power.html Albano Y Romina Power
escuchar-baladas-de-Armando_Manzanero.html Armando Manzanero
musica-Merengue-de-Banda_Cuisillos.html Banda Cuisillos
I was trying to make the most of the fact that the three first have imagenes/flech.gif and that way leave out the fourth, the thing that imagenes/flech.gif isn't in the same order. Here is my attempt to solve it where i get up to the href but include the fourth.
Thanks for any help
You should use an html parser and not a regex, try this:
<?php
$html = <<< EOF
<img border="0" src="imagenes/flech.gif" width="6" height="8">
Albano Y Romina Power<br>
<img border="0" src="imagenes/flech.gif" width="6" height="8">
Armando Manzanero<br>
<a name="inicio21" href="musica-Merengue-de-Banda_Cuisillos.html">
<img border="0" src="imagenes/flech.gif" width="6" height="8">Banda Cuisillos</a><br>
Baladas Alternativas<br>
EOF;
$dom = new DOMDocument();
#$dom->loadHTML($html);
# Iterate over all the <a> tags
foreach($dom->getElementsByTagName('a') as $link) {
$url = $link->getAttribute('href');
$text = preg_replace('/[\r\n]/sm', '', $link->nodeValue); // remove line breaks
//if doesn't contain the banned words...
if (!preg_match('/(Baladas Alternativas|another text to filter)/sm', $text)) {
echo $url ." ".$text. "\n";
}
}
?>
DEMO
http://ideone.com/5QX83x
RESOURCES
http://htmlparsing.com/php.html
this code will get the first 3 links
$a='<img border="0" src="imagenes/flech.gif" width="6" height="8">Albano Y Romina Power<br><img border="0" src="imagenes/flech.gif" width="6" height="8">Armando Manzanero<br><a name="inicio21" href="musica-Merengue-de-Banda_Cuisillos.html"><img border="0" src="imagenes/flech.gif" width="6" height="8">Banda Cuisillos</a><br>Baladas Alternativas<br>';
preg_match_all('/<a.*?href="(.+?)">(?:<img.*\d+">)?(.+?)<\/a>/',$a,$match);
echo $match[1][0] . " " . $match[2][0]."<br>";
echo $match[1][1] . " " . $match[2][1]."<br>";
echo $match[1][2] . " " . $match[2][2]."<br>";

how to store in a variable in its script format as string using php

I want to store this script below as a variable using a text area form but when I do and echo the variable, the browser interprets the code. I don't want this, I just want to store the script "as is" in a variable so I can use that variable for further manipulation. any ideas how I can accomplish this using a text area form in php?
<img border="0" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&ASIN=B008EYEYBA&Format=_SL110_&ID=AsinImage&MarketPlace=US&ServiceVersion=20070822&WS=1&tag=mytwitterpage-20" ><img src="http://www.assoc-amazon.com/e/ir?t=mytwitterpage-20&l=as2&o=1&a=B008EYEYBA" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />
btw, below is the exact php variable that I want when the user put the script above in a text area.
$str = "<img border="0" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&ASIN=B008EYEYBA&Format=_SL110_&ID=AsinImage&MarketPlace=US&ServiceVersion=20070822&WS=1&tag=mytwitterpage-20" ><img src="http://www.assoc-amazon.com/e/ir?t=mytwitterpage-20&l=as2&o=1&a=B008EYEYBA" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />";
any help is greatly appreciated. thanks
use like this
<?php
$str = '<img border="0" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&ASIN=B008EYEYBA&Format=_SL110_&ID=AsinImage&MarketPlace=US&ServiceVersion=20070822&WS=1&tag=mytwitterpage-20" ><img src="http://www.assoc-amazon.com/e/ir?t=mytwitterpage-20&l=as2&o=1&a=B008EYEYBA" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />' ;
echo htmlentities($str);
?>
If you need to output your value in HTML, you need to replace your < and > with < and >. Best is to use htmlspecialchars() function for that.
Also you were told correctly that your string has unescaped quotes in it (replace inside quotes with \").

how to get Hyperlink in PHP for echo statements

I have piece of php code which I want to put as a link.
echo"<img border='1' src='$images[$i]' width='350' height='250'>";
This is what I came up with
<a href = "something"> above statement <a>
but this gives me error.
<?php echo "<a href = 'something'><img border='1' src='".$images[$i]."' width='350' height='250'></a>"; ?>
echo"<img border='1' src='$images[$i]' width='350' height='250'>";
echo "<a href="url" > <img border='1' src='".$images[$i]."' width='350' height='250'> </a>";
OR
<a href="url" > <?php echo "<img border='1' src='".$images[$i]."' width='350' height='250'>"; ?></a>
echo"
<a href='...'>
<img border='1' src='{$images[$i]}' width='350' height='250'>
</a>
";
You can use the concatenate dot operator in this way:
<?php
echo '<img border="1" width="350" height="250" src="'.$images[$i].'"'>;
?>

Putting image tags into php

Hello im using page nation which is amazing but now im trying to print off there avaratar im using this code
echo "<IMG SRC=\"$list['avatar']\" WIDTH=\"268\" HEIGHT=\"176\"
BORDER=\"0\" ALT=\"\" \/>";;
but im getting this error
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in test.php on line 64
Try
echo "<IMG SRC=\"".$list['avatar']."\" WIDTH=\"268\" HEIGHT=\"176\"
BORDER=\"0\" ALT=\"\" />";
instead, or you could use this one, too
echo "<IMG SRC=\"{$list['avatar']}\" WIDTH=\"268\" HEIGHT=\"176\"
BORDER=\"0\" ALT=\"\" />";
or better and readable ones:
echo '<IMG SRC="'.$list['avatar'].'" WIDTH="268" HEIGHT="176" BORDER="0" ALT="" />';
echo '<IMG SRC="', $list['avatar'], '" WIDTH="268" HEIGHT="176" BORDER="0" ALT="" />';
I prefer this
echo "<IMG SRC=\"$list[avatar]\" WIDTH=\"268\" HEIGHT=\"176\" BORDER=\"0\" ALT=\"\" />";
eliminate de braces surrounding the variable and the single quotes for the array key
this should work :
echo '<IMG SRC="'.$list['avatar'].'" WIDTH="268" HEIGHT="176"
BORDER="0" ALT="" />';
to avoid all thet mess
?><IMG SRC="<?=$list['avatar']?> " WIDTH="268" HEIGHT="176" BORDER="0" ALT="" /><?php
and don't post your usual "parse error" comment here.
but check your other PHP syntax issue somewhere else
Try changing it to:
echo "<IMG SRC=\"{$list['avatar']}\" WIDTH=\"268\" HEIGHT=\"176\" BORDER=\"0\" ALT=\"\" />";
...or, less confusingly:
echo '<IMG SRC="'.$list['avatar'].'" WIDTH="268" HEIGHT="176" BORDER="0" ALT="" />';
Try it like this:
echo '<IMG SRC="'.$list['avatar'].'" WIDTH="268" HEIGHT="176" BORDER="0" ALT="" />';

Categories