I'm new to PHP (so no bullying please) and I'm trying to trim a variable to remove quotation marks. For some reason, my quotation marks are replaced by semicolons, anybody knows why ?
Where $movieArray["title"] = '"Veronica Mars"';
str_replace('"', '', $movieArray["title"]);
output: ;Veronica Mars;
Also, the reason I have this " instead of this " is that the trim doesn't work with ".
Thanks
Your text is not "Veronica Mars". It's probably this:
"Veronica Mars"
If you strip ", only the ; remains.
What you see in the browser screen is the result of rendering some HTML code.
Related
I see this question has been asked a few times but I couldn't find an answer in php.
I have the following string.
$myString = " LLC."
I run this trim($myString);
and I get this back " LLC."
according to the trim documentation it should delete the white space in the front and the back? What am I missing?
I also tried htis trim($myString, " "); same results
The e2808d at the beginning of bin2hex() output is ZERO WIDTH JOINER character and the reason for trim() to not trim it. Try (PHP 7):
echo trim($myString, "\u{200d} \t\n\r\0\x0B");
I think this is happening because of the empty space is not really a real white space (tab) looks like a hidden character (â). i copy your variable and value code into an online PHP editor and i got this:
I'm trying to use preg_split() but the results aren't what I expect to get from the function.
I'm new to php and the whole preg_split() scene and it seems to complicated for me to understand, at least for the moment.
$row = "EL10,40,2019-02-06,55555,2019-01-06,ar#email.com,"Text , random text , 52555885/ 48484848484",Yes,One Two,Broke,2019-01-01,000.00,0.00,0.0,0.0,0.0,0.00,0.00,0.0,VRA "Morning";
$row_expl = preg_split('/(?:[^"]*"|)\K\s*(,\s*|$)/',$row);
I expect to remove comma delimiters while leaving commas in quotation marks.
Everything almost seems to work, the only problem occurs at the very end.
It adds extra quotation marks to: VRA "Morning".
The result seems like this: "VRA ""Morning"""
A regex actually is the wrong tool for your problem. A CSV parser that defines the delimiter and enclosure character is the tool you need.
str_getcsv('EL10,40,2019-02-06,55555,2019-01-06,ar#email.com,"Text , random text , 52555885/ 48484848484",Yes,One Two,Broke,2019-01-01,000.00,0.00,0.0,0.0,0.0,0.00,0.00,0.0,VRA "Morning')
The default delimiter for the str_getcsv is , and the enclosure character is " so should be all set with the default options. You can see more about the function here http://php.net/manual/en/function.str-getcsv.php.
https://3v4l.org/GFWkr
I have an issue. I'm trying to write a string with ASCII text like this: '/\'. But whenever I do that the backslash screws up the code by canceling out the quote defining it a string therefore screwing it up. Is there anyway to cancel out the backslash so it doesn't cancel out the quote? Thanks guys!
The \ is special character, that says: 'The next character has special meaning'.
So if you want to dispaly \ you should write... \\ to get one \ in output
It would be very helpful to show what you have tried, but this will produce the exact output you requested (as shown by SO)
echo '\'/\\' . "'\n" ;
'/\'
It should also give you an idea of how backslash escaping works in different types of strings.
A great solution when writing stuff like that is HEREDOC. Inside a heredoc block you don't need to worry about escaping anything, it will just be text.
For example:
echo <<<TEXT
/|\/|\/|\/|\/|\/|\/|\/|\/|\/|\
TEXT;
There is one catch. PHP will break if you don't align the echo at the start of the line, or if the TEXT; is not aligned at the start of the line.
Heredoc can also be assigned to a variable, like so:
$var = <<<SOME_MORE_TEXT
/|\/|\/|\/|\/|\/|\/|\/|\/|\/|\
SOME_MORE_TEXT;
Finally, HEREDOC preserves tabs and spaces. Which also might come in handy when doing ASCII art.
Refer to: http://php.net/manual/en/language.types.string.php for more information.
You only need to escape the final one when using single quotes.
$var = 'backslash\backslash\backslash\\';
// output is:
// backslash\backslash\backslash\
I have searched throughout this site and google, and could not find a viable solution. There are solutions, but none seem to work for me.
I have a text area that serves as the input for a form, as well as the output (editable) when displaying the form to edit. So, I have a textarea like so:
echo '<textarea>'.$resolution.'</textarea>';
If I enter something with line breaks, it is interpreted as \r\n wherever there is a carriage return. Example:
Input:
This is a
test.
Output:
This is a\\r\\n\\r\\ntest.
Now, I found it simple to remove the extra slash by using stripslashes as follows:
$resolution = stripslashes($resolution);
...but, now the output is:
This is a\r\n\r\ntest.
I cannot figure out how to convert \r\n to a line break (that is, without using a tag, since that would only output <br> within the textarea, where html would not be supported. I've tried all of the following, but none of them worked:
//Effort 1
$resolution = trim($resolution);
//Effort 2
$resolution = nl2br($resolution);
//Effort 3
$resolution = htmlentities($resolution);
//Effort 4
$resolution = preg_replace("\\r\\n","<br>",$resolution);
I'm now at a complete loss. Can anyone shed some light on this?
PHP has two different string building modes. The first uses single quotes, and will do absolutely no variable or special character substitutions. That's what you're using.
The second is variable-embedding in double quoted strings, which should work:
$text = str_replace("\\", "\", $text);
echo "<textarea>$text</textarea>";
The \r and \n should now be active carriage return and newline characters in your output, not the character "slash" and then an 'r' or 'n'
Just use double quotes on str_replace
str_replace('\r',"\r",str_replace('\n',"\n",$resolution));
http://php.net/manual/en/language.types.string.php
or if you're really dealing with double backslashes:
str_replace('\\\\r',"\r",str_replace('\\\\n',"\n",$resolution));
On my home page, I display some info about the hikes (it is a hiking site) and embarassinlgy,
the \r\n characters still show up.
I do these functons
$hike_description = htmlspecialchars ($hike_description);
$hike_description = nl2br($hike_description);
And still those characters do not go away. You can take a look yourself at http://www.comehike.com
Would you know what is the proper way to get rid of the \r\n characters? You can see it happening in the "Upcoming Hikes" section...on the 3rd hike.
Thanks!
In your case, the following will work:
$hike_description = str_replace ( "\\r\\n","<br />", $hike_description);
The text \r\n is literal, rather than control characters.
Try manually replacing the sequence
str_replace( "\r\n", "<br />", $hike_description );
The \n\r in your page are not escape sequences... They are actually a \ character followed by a n character followed by a \ character followed by a r character.
In your database, you should store that as the actual characters, not the escape sequences. Then, calling nl2br() will work as expected.
Yes, you can do a str_replace(), however, you should instead fix the encoding of your data in your database. It will save you trouble in the future.