Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace " ’ " with " ' " in PHP

I'm grabbing a string from the database that could be something like String’s Title however I need to replace the with a ' so that I can pass the string to an external API. I've used just about every variation of escaped strings in str_replace() that I can think of to no avail.

like image 480
Mike R Avatar asked Jul 05 '12 18:07

Mike R


People also ask

How can I replace one string with another in PHP?

The str_replace() is a built-in function in PHP and is used to replace all the occurrences of the search string or array of search strings by replacement string or array of replacement strings in the given string or array respectively.

What is use of trim in PHP?

Definition and Usage. The trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string.

How do I remove a word from a string in PHP?

Answer: Use the PHP str_replace() function You can use the PHP str_replace() function to replace all the occurrences of a word within a string.

How do you use implode?

The implode() function returns a string from the elements of an array. Note: The implode() function accept its parameters in either order. However, for consistency with explode(), you should use the documented order of arguments. Note: The separator parameter of implode() is optional.


1 Answers

$stdin = mb_str_replace('’', '\'', $stdin);

Implementation of mb_str_replace() here: http://www.php.net/manual/en/ref.mbstring.php#107631

I mean this:

<?php
function mb_str_replace($needle, $replacement, $haystack) {
   return implode($replacement, mb_split($needle, $haystack));
}
echo mb_str_replace('’', "'", "String’s Title");

It may solve encoding problems.

like image 87
pamil Avatar answered Oct 06 '22 01:10

pamil