Im trying to do this
$input = "<img src="HTML/images/user.png" alt="" />";
but it does not work out,
i know im supposed to put a / before a " or something please help
Try this
$input = "<img src='HTML/images/user.png' alt='' />";
Or
$input = "<img src=\"HTML/images/user.png\" alt=\"\" />";
all you need to do is escape double quote.
just like below.
\"HTML/images/user.png\" alt=\"\"
Note that you have use " for PHP already.
You can either choose to use ' or \".
For example,
$input = "<img src='HTML/images/user.png' alt='' />";
$input = "<img src=\"HTML/images/user.png\" alt=\"\" />";
Try this
$input = "<img src='HTML/images/user.png' alt='' />";
I am trying to replace image links, youtube video links and regular links separately with appropriate html tags and im having trouble targeting just the regular links:
Here's my code:
function($text)
{
$output = preg_replace('#(http://([^\s]*)\.(jpg|gif|png))#',
'<br/><img src="$1" alt="" width="300px" style = "clear:both;"/><br/>', $text);
$output = preg_replace('#(http://([^\s]*)youtube\.com/watch\?v=([^\s]*))#',
'<br/><iframe width="480px" height="385px" src="http://www.youtube.com/embed/$3"
frameborder="0" allowfullscreen style = "clear:both;"></iframe><br/>', $output);
$output = preg_replace('#(http://([^\s]*)\.(com))#',
'<br/>$1<br/>', $output);
return $output
}
This is instead replacing all links in the last step...how do i avoid this and replace only links (that arent youtubes or images) in the last step?
Thanks!
The urls your looking for need to be delimited by something such as spaces or new lines. Just add your delimiter to the regexes, so the the last regex is not too greedy! eg...
<?php
$text = "
http://www.youtube.com/watch?v=yQ4TPIfCK9A&feature=autoplay&list=FLBIwq18tUFrujiPd3HLPaGw&playnext=1\n
http://www.asdf.com/asdf.png\n
http://www.asdf.com\n
";
var_export($text);
var_export(replace($text));
function replace($text)
{
$output = preg_replace('#(http://([^\s]*)\.(jpg|gif|png))\n#',
'<br/><img src="$1" alt="" width="300px" style = "clear:both;"/><br/>', $text);
$output = preg_replace('#(http://([^\s]*)youtube\.com/watch\?v=([^\s]*))\n#',
'<br/><iframe width="480px" height="385px" src="http://www.youtube.com/embed/$3"
frameborder="0" allowfullscreen style = "clear:both;"></iframe><br/>', $output);
$output = preg_replace('#(http://([^\s]*)\.(com))\n#',
'<br/>$1<br/>', $output);
return $output;
}
You could use preg_replace_callback to match each link just once. So you don't have to worry about modifying a link twice:
$input = <<<EOM
http://www.example.com/fritzli.jpeg
https://www.youtube.com/watch?v=blabla+bla+"+bla
http://wwww.google.com/search?q=blabla
EOM;
echo replace($input);
function replace($text) {
$output = preg_replace_callback("#(https?://[^\s]+)#", function($umatch) {
$url = htmlspecialchars($umatch[1]);
if(preg_match("#\.(jpg|jpeg|gif|png|bmp)$#i", $url)) {
$result = "<br/><img src=\"$url\" alt=\"\" width=\"300px\" "
. " style = \"clear:both;\"/><br/>";
} elseif(preg_match("#youtube\.com/watch\?v=([^\s]+)#", $url, $match)) {
$result = "<br/><iframe width=\"480px\" height=\"385px\""
. " src=\"http://www.youtube.com/embed/{$match[1]}\""
. " frameborder=\"0\" allowfullscreen style = \"clear:both;\">"
. "</iframe><br/>";
} else {
$result = "<br/>$url<br/>";
}
return $result;
}, $text);
return $output;
}
Note: the code above works only with a PHP-version >= 5.3. If you use 5.3 or below you can just extract the inner function to a separate function and supply the function name as an argument to preg_replace_callback:
function replace_callback($umatch) {
$url = htmlspecialchars($umatch[1]);
if(preg_match("#\.(jpg|jpeg|gif|png|bmp)$#i", $url)) {
$result = "<br/><img src=\"$url\" alt=\"\" width=\"300px\" "
. " style = \"clear:both;\"/><br/>";
} elseif(preg_match("#youtube\.com/watch\?v=([^\s]+)#", $url, $match)) {
$result = "<br/><iframe width=\"480px\" height=\"385px\""
. " src=\"http://www.youtube.com/embed/{$match[1]}\""
. " frameborder=\"0\" allowfullscreen style = \"clear:both;\">"
. "</iframe><br/>";
} else {
$result = "<br/>$url<br/>";
}
return $result;
}
function replace($text) {
$output = preg_replace_callback("#(https?://[^\s]+)#",
"replace_callback", // the name of the inner function goes here
$text);
return $output;
}
I'm, trying to get the Joomla topbanner to show 2 different images leading to 2 different links, so I changed the following code,
from this:
// get a parameter from the module's configuration
$imgname = $params->get('imgname');
$imgwidth = $params->get('imgwidth', '');
$imgheight = $params->get('imgheight', '90');
$imgtarget = $params->get('imgtarget', '');
$imgpath = JURI::base().'images/stories/';
$html = "<a href='".$imgtarget."' target='_blank'>";
$html .= "<img src='".$imgpath.$imgname."' border='0' alt='' width='".$imgwidth."' height='".$imgheight."'>";
$html .= "</a>";
echo $html;
to this:
// get a parameter from the module's configuration
$imgname = $params->get('imgname');
$imgwidth = $params->get('imgwidth', '');
$imgheight = $params->get('imgheight', '90');
$imgtarget = $params->get('imgtarget', '');
$imgpath = JURI::base().'images/stories/';
//split up image and targets
list($image1,$image2) = explode(',',$imgname);
list($target1,$target2) = explode(',',$imgtarget);
//steps
//divide image into 2 using any image editing tool
//upload to server
//set up images separated by comma in admin
//add second target to admin separated by comma
$html = "<a href='".$target1."' target='_blank'>";
$html .= "<img src='".$imgpath.$image1."' border='0' alt='' width='".$imgwidth."' height='".$imgheight."'>";
$html .= "</a>";
$html = "<a href='".$target2."' target='_blank'>";
$html .= "<img src='".$imgpath.$image2."' border='0' alt='' width='".$imgwidth."' height='".$imgheight."'>";
$html .= "</a>";
echo $html;
I edited it in the back end with a delimiter but it is only reading the second image and link.
Any help please?
The issue is with this line:
$html = "<a href='".$target2."' target='_blank'>";
You're not cocatenating $html, you're reassigning it. All the old information is lost. Simply do this:
$html .= "<a href='".$target2."' target='_blank'>";
SOLVED: Read the comments below #Eray.
I have a PHP function to look through text and convert text emoticons to images. :), :(, :|, etc. I also have a function that looks through text and replaces BBCode with HTML. I execute these on a string from a database. Both of these use the global variable $newtext.
emoticon($row['words']);
bb($row['words']);
echo "<b>" . $row['username'] . "</b> - " . $row['time'];
echo "<p>" . $newtext . "</p>";
echo "";
The odd thing about this, is that now (I can't remember what I did) the emoticon function doesn't work, but the bb function does. By doesn't work, I mean doesn't replace anything. Text remains text. This had worked before. Also, every few times, $newtext comes before the username. Here are my functions...
function emoticon($text)
{
global $newtext;
$newtext=str_replace(":)", "<img src='emoticons/smile.gif'>", $text);
$newtext=str_replace(":(", "<img src='emoticons/sad.gif'>", $newtext);
$newtext=str_replace(":D", "<img src='emoticons/biggrin.gif'>", $newtext);
$newtext=str_replace(":p", "<img src='emoticons/tongue.gif'>", $newtext);
$newtext=str_replace(":P", "<img src='emoticons/tongue.gif'>", $newtext);
$newtext=str_replace(":|", "<img src='emoticons/neutral.gif'>", $newtext);
$newtext=str_replace("8)", "<img src='emoticons/cool.gif'>", $newtext);
$newtext=str_replace("8D", "<img src='emoticons/cool.gif'>", $newtext);
$newtext=str_replace(":o", "<img src='emoticons/surprised.gif'>", $newtext);
$newtext=str_replace(":O", "<img src='emoticons/surprised.gif'>", $newtext);
$newtext=str_replace(";)", "<img src='emoticons/wink.gif'>", $newtext);
$newtext=str_replace("^<**>^", "<img src='emoticons/crab.gif'>", $newtext);
}
function bb($text)
{
global $newtext;
$array=array(
"[b]" => "<b>",
"[/b]" => "</b>",
"[i]" => "<i>",
"[/i]" => "</i>",
"[u]" => "<u>",
"[/u]" => "</u>",
"[big]" => "<h1>",
"[/big]" => "</h1>",
);
$newtext = str_ireplace(array_keys($array), array_values($array), $text);
}
Could you explain or help me? Also, is there a better way than using global variables? I know they can be a bit "dangerous."
function emoticon($text)
{
$smiley = array(
':)',
':(',
);
$replace = array(
"<img src='emoticons/smile.gif'>",
"<img src='emoticons/sad.gif'>",
);
return str_replace($smiley, $replace, $text);
}
I think you'd better do this:
$text = $row['words'];
$text = emoticon($text);
$text = bb($text);
echo "<b>" . $row['username'] . "</b> - " . $row['time'];
echo "<p>" . $text . "</p>";
echo "";
And then edit your functions like this:
function emoticon($text)
{
// remove this line: global $newtext;
$text=str_replace(":)", "<img src='emoticons/smile.gif'>", $text);
// etc...
$text=str_replace("^<**>^", "<img src='emoticons/crab.gif'>", $text);
return $text;
}
function bb($text)
{
// remove this line: global $newtext;
$array=array(
// etc...
);
return str_ireplace(array_keys($array), array_values($array), $text);
}
First, DEFINE your PHP functions:
function emoticon ( $string )
{
$emoticons = array( ':)' , ';)' );
$icons = ('happy.gif','wink.gif');
return str_replace( $emoticons, $icons , $string );
}
function bb( $string )
{
//BOLD [b]text[/b]
$string = preg_replace('/(\[b\]([\w\d\s\.]+)\[\/b\])/i','<b>$2</b>',$string);
//ITALIC [i]text[/i]
$string = preg_replace('/(\[i\]([\w\d\s\.]+)\[\/i\])/i','<em>$2</em>',$string);
//UNDERLINE [u]text[/u]
$string = preg_replace('/(\[u\]([\w\d\s\.]+)\[\/u\])/i','<u>$2</u>',$string);
return $string;
}
Second, Call you PHP defined functions:
echo bb( "[b]Bold[/b]" ); //Return <b>Bold</b>
Bold
echo bb( "[i]Italic[/i]" ); //Return <em>Italic</em>
Bold
echo bb( "[i]My [b]Text[/b][/i]" ); //Return <em>My <b>Text</b></em>
My Text
$var = $_POST['foo'];
echo bb( $var );
Third, Test your code:
emoticon($row['words']);
bb($row['words']);
echo "<b>" . $row['username'] . "</b> - " . $row['time'];
echo "<p>" . $newtext . "</p>";
echo "";
didnt test it but this should do:
notice that instead of using global $newtext i'm creating a local variable and return it to the outer scope via return.
function emoticon($text) {
$newtext=str_replace(":)", "<img src='emoticons/smile.gif'>", $text);
$newtext=str_replace(":(", "<img src='emoticons/sad.gif'>", $newtext);
$newtext=str_replace(":D", "<img src='emoticons/biggrin.gif'>", $newtext);
$newtext=str_replace(":p", "<img src='emoticons/tongue.gif'>", $newtext);
$newtext=str_replace(":P", "<img src='emoticons/tongue.gif'>", $newtext);
$newtext=str_replace(":|", "<img src='emoticons/neutral.gif'>", $newtext);
$newtext=str_replace("8)", "<img src='emoticons/cool.gif'>", $newtext);
$newtext=str_replace("8D", "<img src='emoticons/cool.gif'>", $newtext);
$newtext=str_replace(":o", "<img src='emoticons/surprised.gif'>", $newtext);
$newtext=str_replace(":O", "<img src='emoticons/surprised.gif'>", $newtext);
$newtext=str_replace(";)", "<img src='emoticons/wink.gif'>", $newtext);
$newtext=str_replace("^<**>^", "<img src='emoticons/crab.gif'>", $newtext);
return $newtext;
}
function bb($text) {
$array=array(
"[b]" => "<b>",
"[/b]" => "</b>",
"[i]" => "<i>",
"[/i]" => "</i>",
"[u]" => "<u>",
"[/u]" => "</u>",
"[big]" => "<h1>",
"[/big]" => "</h1>",
);
return str_ireplace(array_keys($array), array_values($array), $text);
}
$row['words'] = emoticon( $row['words'] );
$row['words'] = bb( $row['words'] );
I am trying to convert a pre-existing site that had html and php intermingled into a Smarty template based site. I never used Smarty before so this is proving very difficult for me. I get that you can assign a variable like so:
$smarty->assign('number_of_items_in_cart', $number_of_items_in_cart);
and use it in the tpl file like so:
{$number_of_items_in_cart}
but what about more complex things like this block of code that I had on the old site:
$query = mysql_query(" SELECT * FROM products WHERE id = '$pid' ");
if (mysql_num_rows($query) == 1) {
while ($row = mysql_fetch_array($query)) {
extract($row);
echo "<h2>$name</h2>";
echo "<img src='images/product_images/$image' alt='' width='100' />";
echo $description;
echo '<p>'.money($price).'</p>';
echo "<input type='text' value='1' class='qty-$id' />";
echo "Add to Cart";
}
} else {
redirect('404.php');
}
How can I work with this in a Smarty template, since the output is within a while loop?
Instead of echoing this, you can append it in a string variable then pass it to smarty:
$string = "";
while ($row = mysql_fetch_array($query)) {
extract($row);
$string .= "<h2>$name</h2>";
$string .= "<img src='images/product_images/$image' alt='' width='100' />";
$string .= $description;
$string .= '<p>'.money($price).'</p>';
$string .= "<input type='text' value='1' class='qty-$id' />";
$string .= "Add to Cart";
}
Now u can pass it to smarty
$smarty->assign('place_holder', $string);
I hope this is what you are looking for
You can use the foreach builtin function to iterate over an array containing your query results.
You could also use foreachelse to display an alternate message (though in this case you're redirecting).
See example 7.9 in http://www.smarty.net/docsv2/en/language.function.foreach .
Edit: there's also a while function if that's what you really want.