I have the following variable which returns my URL as needed. But i need to run str_replace() on it to replace a character before echoing it into my HTML code.
$url = str_replace("%3A", ":", " . nl2br( $row['url']) . ");
As it stands the " . nl2br( $row['url']) . " contains %3A instead of the colon in the URL and for some reason its rendering my links like this
http://www.mydomain.com/http%3A//url.com
I'm not really sure what your question is, but it looks like this is what you want:
$url = urldecode($row['url']);
The %3A is a URL encoded colon (:).
Related
How can i prevent that PHP converts a recognized part of a string to an html-entity?
So e.g. lets say i have to concat parts together to an url, like:
echo '&' . 'section=' . '<br>';
$a = '&a';
$b = 'mplitude=';
echo "{$a}{$b}" . '<br>';
echo sprintf("%s%s", '"e', '=');
the code above prints the following:
§ion=
&litude=
"e=
instead of:
§ion=
&litude=
"e=
how can this be prevented without throwing filters on it trying to convert the symbols back to an string again?
You need using htmlspecialchars function:
echo htmlspecialchars('&' . 'section=' . '<br>');
I have a link that is sent throw some PHP code:
echo "<a href='" . $galerry . "#" . apastro(get_title($primid)) . "' class='linkorange'>voir sa galerie</a>";
$galerry links to another page.
get_title($primid) is the id of a specific element in $galerry page.
And the mechanism works fine until one of the elements id has a single quote in it. Which makes sense as it would interrupt the echo function.
This is why I have the apastro function:
function apastro($phrase){
$phrase1 = str_replace("'", "\'", $phrase);
return $phrase1;
}
Yet, the \ before the single quote isn't helping...
So let's say the link redirects to the element with id="l'aro" on the page something.php. Then the URL will be something.php#l\.
it would interrupt the echo function
It wouldn't. It would break a string literal delimited by ' characters, but your string literal is delimited with " characters. In this case, it is breaking the HTML attribute value which is delimited by ' characters.
\ is not an escape character for URLs or HTML.
Use urlencode to make a string safe to put into a URL.
Use htmlspecialchars to make a string safe to put into an HTML attribute.
$title = get_title($primid);
$urlsafe_title = urlencode($title);
$url = $galerry . "#" . $urlsafe_title;
$htmlsafe_url = htmlspecialchars($url, ENT_QUOTES | ENT_HTML5);
echo "<a href='$htmlsafe_url' class='linkorange'>voir sa galerie</a>";
If you're looking to escape single quotes only, use double backslashes, as follows
$str = str_replace("'", "\\'", $str);
I'm sure I'm missing something obvious here: the following is echoing lat-long variables from MySQL, and the longitude variable begins with a minus sign, which prevents the echo statement from reading it and all that follows it. I'm sure there is a way to clean/escape that but just can't work it out.
echo "http://maps.google.com/maps?ll=" . $row['latitude'] . "," . $row['longitude'] . " target=_new>View in Google Maps";
This is output from a PDO query and testing passing the lat-long into Google Maps.
As I understand, it's a link?
Then, use urlencode for string.
The minus signs are not a problem. You may need to urlencode() because of the comma, but you need quotes around the URL in the href as well:
echo '<br /><a href="http://maps.google.com/maps?ll='
. urlencode($row['latitude'] . ',' . $row['longitude'])
. '" target="_new">View in Google Maps</a>';
I have a variable specified earlier in my script and using the & character, otherwise the script errors out with EntityRef: expecting ';' when using a & character:
$url = "http://www.domain.com/residential?frame=RESI&MLNumber=";
The problem is that later in the script I use this variable like:
echo '<link>' . $url . '' . $title . '</link>';
The problem is that the end result is a bad URL like this:
http://www.domain.com/residential?frame=RESI&base=frames&MLNumber=26524589
How do I resolve this?
In my dynamically generated website sometimes parts of my URLs contain a / character:
Serena Williams/Venus Williams-Andrea Hlavackova/Lucie Hradecka
And, naturally, the URL returns a 404 error as the / sign is considered a folder so the URL doesn't exist.
What would be the PHP function to encode and later decode the string which contains a / character?
You can use PHP: urlencode to encode and its counterpart PHP: urldecode to decode:
urlencode($stringinput)
You can user htmlentities() whit urlencode()
<?php
$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '<a href="index.php?' . htmlentities($query_string) . '">';
?>