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
Related
I have a $_GET[q] from the URL. I am trying to ECHO the search term back into the search box. Sometimes people might submit queries encapsulated in quotes and in these cases the ECHO interprets the search term
ECHO $_GET[q];
as:
ECHO ""search term"";
and as a result I get a blank search box. Search queries with a single quote, like: Peter's house, work fine.
When I use:
mysqli_real_escape_string($conn, $_GET[q])
I only get a backslash in the search box.
How could I populate the search box with a search term encapsulated in double quotes?
You could also use:
echo str_replace('"','',$_GET[q]);
This will of course remove all double quotes, so if they may be valid somewhere in the search term then better might be:
echo str_replace('"','"',$_GET[q]);
Haven't tested this but this might also work:
echo html_entity_decode(htmlentities($_GET[q))
You could use addslashes
like this:
$t = 'peter "pan"';
echo addslashes($t); // outputs: peter \"pan\"
You can try:
$str = "Hello World!";
echo $str . "<br>";
echo chop($str,"");
Ouptput:
Hello World
Explanation:
The chop() function, helps you chop off the quotes anyone might add to string.
You can manipulate as appropriate for your code.
$str = preg_replace( '["|\']','', $_REQUEST['q'] );
echo( $str ); //no double, no single quotes, faster than str_replace when you have to make more than 1 call to str_replace
or...
$str = str_replace( '"','', $_REQUEST['q'] ); //no double quotes, faster than preg_replace when you only make one call to str_replace
echo( $str ); //no double quotes
My code simply displays a random line from a text file,
But in my text file most of the proxies look like this: "11.15.19.15:80" I need help how to display only the address on the site, and remove the quotation marks.
<?php
$message_array = file("proxies.txt");
$message = array_rand($message_array);
echo "$message_array[$message]";
?>
All you need to do is wrap the string in a trim():
<?php
$message_array = file("proxies.txt");
$message = array_rand($message_array);
echo trim($message_array[$message], "\""); // 11.15.19.15:80
?>
Note that the second argument in trim() is needed, because your string contains the " characters, rather than uses them to denote the string itself. Adding an escaped backslash ("\"") removes the quotation marks from what is inside the string itself.
I've created a simple demonstration of this at 3v4l.org here.
I think you can use a regex like so:
preg_replace('/["]*/g', '', $message);
Use str_replace. Here is an example:
$message = str_replace('"', '',$message);
You can use the trim function if you just need to remove the double quotes:
$message = trim($message, '"');
Also str_replace:
$message = str_replace('"', '', $message);
I am trying to go through the array string I called out from database and filtered to a readable state. The string could have a lot of \' and \", below is just an example.
$content = 'It\'s go to somewhere \"GREAT\"!';
I am trying to use str_replace but it is not working...
$content1= str_replace('\\\'', "'", $content );
$newcontent= str_replace('\\\"', '"', $content1 );
Output should be
It's go to somewhere "GREAT"!
instead.. I get
It\'s go to somewhere \"GREAT\"!
I looked at preg_replace, but I don't quite get all the /.. or where to start on it.
Please help.
Here's how
$content = 'It\'s go to somewhere \"GREAT\"!';
$content = stripslashes($content);
echo $content;
What you want to use is stripslashes($str).
Returns a string with backslashes stripped off. (\' becomes ' and so on.) Double backslashes (\) are made into a single backslash ().
$str = "Is your name O\'reilly?";
// Outputs: Is your name O'reilly?
echo stripslashes($str);
I have problem in sending message to my client via socket,the string that I would like to send is like this "##w32,12345678,xxx,5*zy\r\n"
$msg = $_POST['comm_input']; //"##w32,12345678,xxx,5*zy\r\n"
if this is posted i get the value of $msg which is "##w32,12345678,xxx,5*zy\r\n"
but my client will not accept this kind of message..but if I manually do like this without posting the comm_input;
$testmsg = "##w32,12345678,xxx,5*zy\r\n";
It works fine,I tried to look at in firebug there is no double quotes and \r\n.and it works fine.
if I post the comm_input.and look at in the firebug there is double quotes and \r\n,how can I remove this.
You can use str_replace function to remove \r\n.
DEMO
<?php
$testmsg = "##w32,12345678,xxx,5*zy\r\n"; <-- $_POST value
$order = "\r\n";
$replace = "";
$newstr = str_replace($order, $replace, $testmsg);
echo $newstr; //outputs ##w32,12345678,xxx,5*zy
?>
using str_replace, you need to escape the \ with and extra \, hence, \r as string becomes \\r
$msg = $_POST['comm_input']; //"##w32,12345678,xxx,5*zy\r\n" ;
$new_msg = str_replace("\\r\\n", "", $msg);
Edit: to remove double quotes
$new_msg = str_replace('"', "", $new_msg);
Consider reading this article : Escape Sequence in PHP
you can use
$msg = "##w32,12345678,xxx,5*zy\r\n";
$str = rtrim($msg);
Refer trim() and rtrim()
$city = 'London Paris Lisabona';
And i need print this string in textarea.
How print city in new line?
I need in textarea get this:
London
Paris
Lisabona
Code:
$city = 'London\nParis\nLisabona';
echo '<textarea>'.$city.'</textarea>';
result:
London\nParis\nLisabona
In general: Use \n for line breaks.
In your case (only works of cites don't consist of two words, i.e. each word must be a city):
$city = str_replace(' ',"\n", $str); // generates 'London\nParis\nLisabona'
Or if possible build the string with \n instead of spaces from the beginning.
Update:
Escaped character sequences like \n are only processed in double quoted strings. They are taken literally in single quoted strings (with two exceptions). Read more in the documentation:
To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash, double it (\\). All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning.
Thus, you have to declare your strings as
$cities = "London\nParis\nLisabona";
Further note:
Whenever possible avoid echoing HTML with PHP. It makes it more difficult to debug the HTML. Instead, embed the PHP into HTML like so:
<?php
$cities = "London\nParis\nLisabona";
?>
<textarea><?php echo $cities; ?></textarea>
<?php
$city = "London\nParis\nLisabona";
?>
<textarea rows="3" cols="20">
<?php echo $city; ?>
</textarea>
$city = str_replace(' ', "<br />", $city);
If you echo it in HTML.
<textarea><?= str_replace(" ", "<br />", $city); ?></textarea>
If you want "\n" to be converted to line breaks, you need to use double-quotes instead of single quotes.
i.e.
$foo = 'a\nb\nc\n';
echo $foo;
> a\nb\nc\n
$foo = "a\nb\nc\n";
echo $foo;
> a
> b
> c
works in <textarea> form me, lines might get soft-wrapped though (but that is expected)