How to convert apostrophe or quotes in url string? - php

I am trying to convert the apostrophe in URL string using htmlentities() or htmlspecialchars() .... but it is not working for me...
I have following code:
<?php
$new = htmlspecialchars("<a href='http://abc.test.net/content/22799-mdsap-partners-with-sap’s-‘moving-experience’-initiative-in-the-uae-and-oman'>Test</a>");
echo $new;
?>
but I am getting the output from $new:
<a href='http://abc.test.net/content/22799-mdsap-partners-with-sap’s-‘moving-experience’-initiative-in-the-uae-and-oman'>Test</a>
How to convert the apostrophe and single quotes in url...

Try to use urlencode("your URL") only on the part you need (otherwise it will mess up the rest of the URL):
$new = "<a href='http://abc.test.net/content/" . urlencode("22799-mdsap-partners-with-sap’s-‘moving-experience’-initiative-in-the-uae-and-oman") . "'>Test</a>";
echo $new;

Related

Error getting part of url separated by blackslash

I want to get last part of url separated by blackslash
Here is my code
<?php
$str = "C:\xampp\htdocs\sanimailer\storage\mail-attachments\10819_21098672691424384960_C
opy_of_tristan_data_cleanup_version_FBL_3.2.xlsx";
echo basename($str);
?>
I want output as this
10819_21098672691424384960_Copy_of_tristan_data_cleanup_version_FBL_3.2.xlsx
But I am getting output as this
Not sure why I am getting such output .
You need to convert " to ' and then it will work:-
<?php
$str = 'C:\xampp\htdocs\sanimailer\storage\mail-attachments\10819_21098672691424384960_C
opy_of_tristan_data_cleanup_version_FBL_3.2.xlsx';
echo basename($str);
?>
Output:- https://eval.in/829073
The problem you are facing because "\10" converted to that symbol
check here:- https://eval.in/829077

special character in PHP SOAP client

I have text values in an XML document created by the PHP SOAP client that includes an ' (that's an apostrophe). I've confirmed that the encoding is ASCII which should be valid UTF-8. I did it like this:
foreach ($myarrayofstrings as $s){
echo mb_detect_encoding($s);
}
If I remove the apostrophe like this then the server accepts my request.
$myarrayofstrings = str_replace("'", "", $myarrayofstrings);
Question:
Is there anything I can do to ensure this works every time without the str_replace?
For me always work utf8_encode function instead any replace:
$myarrayofstrings = utf8_encode($myarrayofstrings);
But i think you can use htmlspecialchars too
<?php
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
?>

Why is this not making a plus sign?

I have:
$str = 'test%2B';
echo html_entity_decode($str);
I want it to return test +
What am I doing wrong?
NOTE: Sorry, the string cannot be modified. It's from an external source, I just need to make it replace the %2B with + signs somehow with PHP.
You didn't escape the space, and you should be using urldecode instead of html_entity_decode.
Try
$str = 'test%20%2B';
echo urldecode($str); // test +
If you wish to use html_entity_decode, use +:
$str = 'test +';
echo html_entity_decode($str); // test +
EDIT: If you need to decode a url that you cannot change yourself, urldecode should still work fine.
That string is encoded for a URL, not with HTML entities.
You need urldecode.
echo urldecode($str); // "test +"
An HTML-encoded string would look like this: test +, because none of those characters need HTML-encoding.
Try + instead. In your example, you are using URL encoding syntax and not HTML entity syntax.
In html a + is +. Try
$str = 'test +';
$str = "test %2B";
echo urldecode($str);

How to output a string with a double quotation mark?

I need to output a string, which is basically a java code:
I have something like this:
$web = "...if (url.contains('.mp4'))..."
I need that the single quotation mark, will be a double one, and not in html code.
Is it possible to do it?
$new_str = str_replace('\'', '"', $web);
You could optional do it by modifying the actual string (note the use of \ to escape the quotation marks):
$web = "...if (url.contains(\".mp4\"))..."
You can use like this
$web = "...if (url.contains(\".mp4\"))...";
Short of doing a strtr() replacement, just escape your double quotes:
$web = "...if (url.contains(\".mp4\"))..."
See http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double for the complete list of escaping rules.
You should use this
<?php
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
?>
see http://docs.php.net/manual/en/function.htmlspecialchars.php

How to display XML in HTML in PHP?

I have a string with XML:
$string =
"
<shoes>
<shoe>
<shouename>Shoue</shouename>
</shoe>
</shoes>
";
And would like display it on my website like this:
This is XML string content:
<shoes>
<shoe>
<shouename>Shoue</shouename>
</shoe>
</shoes>
So I would like to do it:
on site, not in textbox
without external libraries, frameworks etc.
formatted with proper new lines
formatted with tabs
without colors etc., only text
So how to do it in plain and simple way?
If you just want a plain-text representation of your (pre-formatted) string, you can wrap it in HTML <pre/> tags and use htmlentities to escape the angle brackets:
<?PHP echo '<pre>', htmlentities($string), '</pre>'; ?>
you can use htmlentities(), htmlspecialchars() or some similar function.
It should work like that:
echo '<p>This is XML string content:</p>'
echo '<pre>';
echo htmlspecialchars($string);
echo '</pre>';
If it is a SimpleXMLobject
<pre>
<?php
echo htmlspecialchars(print_r($obj,true));
?>
</pre>
I searched for a solution to have slightly coloured output:
$escaped = htmlentities($content);
$formatted = str_replace('<', '<span style="color:blue"><', $escaped);
$formatted = str_replace('>', '></span>', $formatted);
echo "<pre>$formatted</pre>\n";

Categories