HTML img in PHP variable - php

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='' />";

Related

String_replace replaces not all elements

I am making a Laravel Helper function, that should convert ":)" and others to normal smiles, however, some elements just do not convert! Maybe you can help me to find that, maybe stupid, mistake.
Code:
public static function convertSmiles($string)
{
$smiles = [
':)',
':angel:',
':angry:',
'8-)',
':\'(',
':ermm:',
':D',
'<3',
':(',
':O',
':P',
';)',
':alien:',
':blink:',
':blush:',
':cheerful:',
':devil:',
':dizzy:',
':getlost:',
':happy:',
':kissing:',
':ninja:',
':pinch:',
':pouty:',
':sick:',
':sideways:',
':silly:',
':sleeping:',
':unsure:',
':woot:',
':wassat:'
];
$urls = [
"<img src='".url('/jquery/sceditor/emoticons')."/smile.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/angel.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/angry.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/cool.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/cwy.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/ermm.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/grin.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/heart.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/sad.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/shocked.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/tongue.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/wink.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/alien.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/blink.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/blush.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/cheerful.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/devil.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/dizzy.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/getlost.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/happy.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/kissing.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/ninja.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/pinch.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/pouty.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/sick.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/sideways.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/silly.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/sleeping.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/unsure.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/w00t.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/wassat.png'/>"
];
return str_replace($smiles,$urls,$string);
}
I have problems with <3 and :'(

preg_replace php image messup

Im trying to get the bbcode image to work. But i have problems with it.
what in the code is wrong and why?
I have this input:
[img]http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg[/img]
and would like it to be:
<img src="http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg">
but the result is:
`Q: How can i make this work properly?
Full code:
function parsebb($body,$userid = false, $user2 = false) {
$find = array(
"#\n#",
"#[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]#is",
"/\[url\=(.+?)\](.+?)\[\/url\]/is",
"/\[b\](.+?)\[\/b\]/is",
"/\[i\](.+?)\[\/i\]/is",
"/\[u\](.+?)\[\/u\]/is",
"/\[farge\=(.+?)\](.+?)\[\/farge\]/is",
"/\[size\=(.+?)\](.+?)\[\/size\]/is",
"/\[font\=(.+?)\](.+?)\[\/font\]/is",
"/\[boks\=venstre\](.+?)\[\/boks\]/is",
"/\[boks\=hoyre\](.+?)\[\/boks\]/is",
"/\[boks\=midten\](.+?)\[\/boks\]/is",
"/\[img\](.+?)\[\/img\]/is",
"/\[email\](.+?)\[\/email\]/is",
"/\[midten\](.+?)\[\/midten\]/is",
"/\[venstre\](.+?)\[\/venstre\]/is",
"/\[hoyre\](.+?)\[\/hoyre\]/is",
);
$replace = array(
"<br />",
"\\0",
"$2",
"<strong>$1</strong>",
"<em>$1</em>",
"<span style=\"text-decoration:underline;\">$1</span>",
"<font color=\"$1\">$2</font>",
"<font size=\"$1\">$2</font>",
"<span style=\"font-family: $1\">$2</span>",
"<div align=\"left\" style=\"text-align:center; width:50%; \">$1</div>",
"<div align=\"right\" style=\"text-align:center; width:50%;\">$1</div>",
"<div align=\"center\" style=\"text-align:center; width:50%;\">$1</div>",
"<img src=\"$1\">",
"$1",
"<div style=\"text-align:center;\">$1</div>",
"<div style=\"text-align:left;\">$1</div>",
"<div style=\"text-align:right;\">$1</div>",
);
$body = htmlspecialchars($body);
$body = preg_replace($specialCodes, $specialCodesReplace, $body);
return $body;
}
The problem is at your
"#[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]#is"
pattern, even [img] ... [/img] is matching with it just, comment them and try again, it will be repleaced in well form or put this pattern line to the end of the array

How can I put something like this in a Smarty template?

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.

How do you echo more than one thing with line break tags?

I am currently using the below code to echo a few different variables and 2 line breaks.
But what I would like to know is how can I echo all of the variables including line breaks into one line of code?
<?php
function findworld($var) {
return strpos($var, "world");
}
$firstvar = "hello world";
$secondvar = findworld($firstvar);
$thirdvar = strlen($firstvar);
echo $firstvar;
echo "<br />";
echo $secondvar;
echo "<br />";
echo $thirdvar;
?>
the concat operator in php is "."
echo $firstvar . "<br />" . $secondvar . "<br />" . $thirdvar;
http://www.php.net/manual/en/language.operators.string.php
You can pass multiple parameters to echo, separated by a comma:
echo $firstvar, "<br />", $secondvar, "<br />", $thirdvar;
To avoid repeating the line break, you could also use implode:
$firstvar = "hello world";
$values = array($firstvar,
findworld($firstvar),
strlen($firstvar));
echo implode('<br />', $values);
You can use string concatenation:
echo $firstvar . "<br />" . $secondvar . "<br />" . $thirdvar;
Like others have said, but with speech marks in the all the correct places ;)
echo $firstvar.'<br />'.$secondvar.'<br />'.$thirdvar;
You don't need to concatenate at all with double quotes, you can just:
echo "$firstvar<br />$secondvar<br />$thirdvar";

Why is '==' not working in PHP?

Both values are '2' but I am not getting a true. Why?
echo $getuser. "<br />";
echo $userurl. "<br />";
if ($getuser == $userurl) {
echo "true <br />";
}
Result
2
2
Instead of echoing them out, use var_dump() to see exactly what is stored in those variables:
var_dump($getuser); echo "<br />";
var_dump($userurl); echo "<br />";
You probably have some stray spaces or other characters which are not easy, or maybe not possible to visually detect.
use
var_dump($getuser);
var_dump($userurl);
Pay attention to the string length. Consider using trim() if needed
may be contain dummy space in that, use like this
echo $getuser. "<br />";
echo $userurl. "<br />";
if (trim($getuser) == trim($userurl)) {
echo "true <br />";
}
When I try
$getuser = 2;
$userurl = 2;
echo $getuser. "<br />";
echo $userurl. "<br />";
if ($getuser == $userurl) {
echo "true <br />";
}
Results:
2
2
true
Works for me. On XAMPP (Windows) and Linux (Apache) alike. So there's problem probably somewhere in configuration or something...

Categories