php str_replace problem with single quote getting converted to &039 - php

I am using str_replace to replace some characters and for some reason the output converts single quotes to &039. I am not trying to replace single quotes at all. What can be causing this?

$v = yourstring;
$newv = str_replace("&039", "'", $v);
Example:
$v = "Hi My Name Is &039George&039";
$newv = str_replace("&039", "'", $v);
echo $newv;
The Output Would Be:
Hi My Name Is 'George'
Now I just hope this helps a little and I hope I understood your question right.

Maybe some sort of conversion could be useful:
$v = $_GET['value'];
$v1 = html_entity_decode($v);

You can convert them back with something like
html_entity_decode(__("Some Text"), ENT_QUOTES, "UTF-8")

Related

Clean url string with backslashes and brackets using php so it is a useable url

I'm returning an array from gravity forms and the value contains a url, but for some reason it's added brackets, quote marks, and backslashes!?
I've looked at preg_replace() but seems a little long winded.
And I've look stripslashes() but it removes all the slashes.
My question is there a simple function solution that makes this string value into a usuable url which I can echo out in my page?
Here is my string value below in its entirety...
["http:\/\/joshbakerson.com\/wp-content\/uploads\/gravity_forms\/1-61ecbcd1ce76f3a9c22cc8ee3d541e5b\/2017\/01\/The-Funniest-moment-ever-when-bear-starts-then-bottles-it.mp4"]
I simply want to convert the string above to this...
http://joshbakerson.com/wp-content/uploads/gravity_forms/1-61ecbcd1ce76f3a9c22cc8ee3d541e5b/2017/01/The-Funniest-moment-ever-when-bear-starts-then-bottles-it.mp4
Any advice on what function I should use would be great thanks.
stripslashes() should work for you, as it only removes the \ characters:
$x = '["http:\/\/joshbakerson.com\/wp-content\/uploads\/gravity_forms\/1-61ecbcd1ce76f3a9c22cc8ee3d541e5b\/2017\/01\/The-Funniest-moment-ever-when-bear-starts-then-bottles-it.mp4"]';
$output = stripslashes($x);
you can then use str_replace() to remove the [, ], and " characters:
$output = str_replace('[','', $output);
$output = str_replace(']','', $output);
$output = str_replace('"','', $output);
echo $output;
// http://joshbakerson.com/wp-content/uploads/gravity_forms/1-61ecbcd1ce76f3a9c22cc8ee3d541e5b/2017/01/The-Funniest-moment-ever-when-bear-starts-then-bottles-it.mp4
Try this:
<?php
$string = 'http:\/\/joshbakerson.com\/wp-content\/uploads\/gravity_forms\/1-61ecbcd1ce76f3a9c22cc8ee3d541e5b\/2017\/01\/The-Funniest-moment-ever-when-bear-starts-then-bottles-it.mp4';
$data = str_replace('\/','\\',$string);
echo $data;
?>

how to add double quotation character in PHP string using str_replace

I have String in PHP like:
[x,y,z]
and I want to change it to
["x", "y", "z"]
I used str_replace but I can't represent the double quotation mark " in it like this
$modified = str_replace("[", "["", $NodeIDs);
I also used \ before it like java but it appears in the output. how can I do this?
You can use double quotes " inside single quotes ':
$modified = str_replace("[", '["', $NodeIDs);
Or escape them:
$modified = str_replace("[", "[\"", $NodeIDs);
Or this might be a better approach to get the desired result:
$letters = explode(',', trim($NodeIDs, '[]'));
$NodeIDs = '["' . implode('","', $letters) . '"]';
Another option is using trim, explode and json_encode:
$output = json_encode(explode(',', trim('[x,y,z]', "[]")));
print_r($output);
//["x","y","z"]
Ideone Demo
Single quotes should do the trick:
$modified = str_replace("[", '["', $NodeIDs);
Good luck to you on your project!
Simply do this
Use trim for removing the [, ] from the string, then use explode function to get the exploded string and then implode them.
$str = '[x,y,z]';
$arr = explode(",", trim($str, "[]"));
echo $str = '["'.implode('","', $arr).'"]'; //["x","y","z"]

php regex how to remove duplicate charactors and make every charactor unique in string

How should one use preg_replace() to replace a string from 'aabbaacc' to 'abc'?
Currently, my code uses str_split() then array_unique() then implode().
I think preg_replace() can achieve this also, but I don't know how.
Thank you for your help.
A regex that seems to work for me is /(.)(?=.*?\1)/. Please test it for yourself here:
http://regexpal.com/
I've also tested it with preg_replace('/(.)(?=.*?\1)/', '', 'aaabbbabc') which returns the expected abc.
Hope this helps :)
This is the closest I got. However, it's basically a copy of :
How do I remove duplicate characters and keep the unique one only in Perl?
<?php
$string = 'aabbaacc';
$new = preg_replace( '/(.)(?=.*?\1)/i','', $string );
echo $new;
?>
Unfortunately, it does not keep the string in the same order. I don't know if that is important to you or not.
try this
$string = 'dbbaabbbaac';
$new = preg_replace_callback( array("/(.)\\1+/"),function($M){print_r($M);return $M[1];}, $string );
$new = preg_replace_callback( array('/(.)(.?\\1)/i','/(.)(.*?\\1)/i'),function($M){return $M[1].trim($M[2],$M[1]);}, $new );
echo $new."\n";
output
dbac
or this with out Regex
$value="aabbaacc";
for($i=0;$i<strlen($value);$i++){
$out[$value[$i]]=$value[$i];
}
echo implode("",$out);
output:
abc

How do I remove the apostrophe before inserting into MYSQL? (not simply escaping them)

i have a program that fetches titles of webpages that sometimes have apostrophes. i don't simply want to comment them out and escape them with \s. how can i get rid of them all together?
str_replace() should work:
$string = str_replace("'", "", $string);
You may use this function to clean up titles from any unwanted characters.
function clean_up( $text ) {
$unwanted = array("'"); // add any unwanted char to this array
return str_ireplace($unwanted, '', $text);
}
Use it like:
$dirty_title = "I don't need apostrophes.";
$clean_title = clean_up($dirty_title);
echo $clean_title; // outputs: I dont need apostrophes.
$string = str_replace("'", "'", $string);
This method is PHP & MySQL safe and doesn't alter the appearance of the string when it's echoed. Useful when dealing with names like O'Brian.
If you want to insert a string that contains apostrophes, add below line in code that will escape them:
$data = str_replace("'", "\'", $data);
if you try to trim before mysql update, or insert into ...
$string = str_replace("'", "", $string);
is fine but mysql can use another sign instead of trimmed one.
Here what I developed is trimming after echoing mysql value.
Reading from mysql:
$result = mysqli_query($conn, "SELECT value1 FROM db WHERE id='1' ");
if (mysqli_num_rows($result) > 0) {while($row = mysqli_fetch_assoc($result)) {
echo str_replace('"', '', $row["value"]);
}
}

How to convert a string to an integer?

<?php
header('Content-type: text/html; charset=gb2312');
include("parser.php");
$html = file_get_html('http://www.souyishop.com/shop_gkq5/1280/products.aspx?sku=1351249&shbid=20358');
$price = $html->find('span[id=Product_main1_Label5]');
$price = str_replace(" ", "", str_replace("¥", "", str_replace("元", "", $price[0])));
echo (int)$price . "<br>";
?>
Basically, I'm trying to fetch the price ¥ 32 元 and I remove the currency and white space. After that I try to convert the string to int so that I can do calculation later. Guess what, I get 0. :(
Somethign like this isnt going to work:
echo intval('¥ 32');
So try replacing all nondigits
echo (int)preg_replace( '~\D~', '', $str );
Use a regular expression in this case:
$re = preg_match('/(\d+)/', $str, $matches);
$price = $matches[1];
That str_replace chain is cumbersome
A more elegant solution is to pull out all numbers with preg_match
$price = preg_match('/[0-9]+/i', $price, $matches);
print_r($matches);
I think you should first echo $price without and int conversion to determine if you are actually replacing the string items you think you are, this is probably why it returns 0.
Chinese encoding requires special handling.
edit:
try using the html code for the char (yen = ¥) see: http://www.starr.net/is/type/htmlcodes.html

Categories