Removing ' from string - php

Currently I have the following strings.
$artist = 'Lookas';
$song = 'Can't Get Enough';
As you can see above, the $song portion contains random text appose to just placing a symbol which should look like this ', how can I solve this?
The title also returns this some times as well.. & appose to returning the proper & symbol.

Those are not "random" characters. It is a html number encoded apostrophe.
<?php
$song = 'Can't Get Enough';
var_dump(mb_convert_encoding($song, 'UTF-8', 'HTML-ENTITIES'));
The output obviously is:
string(16) "Can't Get Enough"

As mentioned in the comment by #Memor-X, those are HTML-entities.
If this is data that you're getting from your own database, then you're using htmlspecialchars() in the wrong place. If not, then I recommend reading up on what that function does, and you'll find out how to decode those entities.

Related

str_replace not working on strange character

I get this string "Holder – 2pcs" from my Wordpress post title using the get_the_title() function then I use str_replace to replace the "–" character but no luck!
str_replace("–","-","Holder – 2pcs");
any help appreciated!
Edit:
(Response to comments)
I had to save the text from $title1=get_the_title(); to .txt file and I noticed that the – saved as – in the txt file ... then I replaced str_replace("–","-","Holder – 2pcs") and it works! the problem is that in my wordpress databse the title contains - char as it should but then when I use get_the_title(); function of wordpress in my code to retrieve the title I get the - char as – which is eventually – I dont know why get_the_title(); causing this issue!
Any thoughts?
Your issue is caused by your "-" character being something else that looks the same.
Step 1:
Ensure that everything is using the same Character set, from your MySQL to your PHP to your input text.
$title1 = iconv(mb_detect_encoding(get_the_title(), mb_detect_order(), true), "UTF-8", get_the_title());
(reference)
Step 2:
Ensure that what you convert is the raw string and not an HTML encoded output
$title2 = html_entity_decode($title1, ENT_NOQUOTES | ENT_HTML5, "UTF-8");
Step 3:
Run the str_replace() function as originally attempted. If there are a range of possible "dash" characters then you can build an array:
$dashes = ['–','–','—','-'];
$title3 = str_replace($dashes,"-",$title2);
(reference)
The code you've shared does work:
var_dump(str_replace("–","-","Holder – 2pcs"));
string(13) "Holder - 2pcs"
If it doesn't, they you're actually running something different. Most likely, your input data contains white space or HTML entities and you're looking at it through browser glasses.
Trying further inspecting your input data with e.g.:
header('Content-Type', 'text/plain');
var_dump("Holder – 2pcs", bin2hex("Holder – 2pcs"));
string(15) "Holder – 2pcs"
string(30) "486f6c64657220e280932032706373"

preg_replace replacing &not in string to funny character

For some reason when preg_replace sees &not in string and replaces it with ¬:
$url= "http://something?blah=2&you=3&rate=22&nothing=1";
echo preg_replace("/&rate=[0-9]*/", "", $url) . "<br/>";
But the output is as follows:
http://something?blah=2&you=3¬hing=1 // Current result
http://something?blah=2&you=3&nothing=1 // Expected result
Any ideas why this is happening and how to prevent it?
& has special meaning when used URIs. Your URI contains &not, which is a valid HTML entity on its own. It's being converted to ¬, hence causing the trouble. Escape them properly as &not to avoid this problem. If your data is fetched from elsewhere, you can use htmlspecialchars() to do this automatically.
Use this & in place of this &
because your &no has special meaning
use this url :
http://something?blah=2&you=3&rate=22&nothing=1
and then do your replace accordingly

Can't make ob_get_clean() string into an integer, says output is an 18-character string

I am trying to obtain a number from a function which only echos text instead of returning it to a variable as follows:
ob_start();
function_to_get_id_number();
$thisIDnumber = ob_get_clean();
If I echo the $thisIDnumber variable, the desired number is printed in the HTML output.
However, running var_dump($thisIDnumber) outputs the following: string(18) "7"
(Assuming the number was 7, although var_dump() reports an 18-character string regardless of what the number might be.)
Any attempt to convert the string to an integer (e.g. (int)$thisIDnumber , or int_val($thisIDnumber), or $thisIDnumber = 0+$thisIDnumber fails and the output is 0)
Running mb_detect_encoding($thisIDnumber) reports the string to be ASCII encoded.
I'm not really sure how to get around this, but would very greatly appreciate any suggestions or insights! Many thanks in advance!
The string(18) part could be explained if the function prints lots of white space (spaces, tabs or even carriage returns) and you inspect var_dump()'s output through a web browser (so it renders as HTML and spaces are collapsed). However, casting to number should ignore regular leading spaces. So there's possibly some other non-printable character out there.
Try this:
ob_start();
function_to_get_id_number();
$thisIDnumber = ob_get_clean();
var_dump($thisIDnumber, bin2hex($thisIDnumber));
The hexadecimal code should give you a clue of what's inside the string.
Update:
$data = '5b777073635f63617465676f72795f69645d';
for($i=0, $len=strlen($data); $i<$len; $i+=2){
echo chr(hexdec(substr($data, $i, 2)));
}
... prints this:
[wpsc_category_id]
:-?
You have not provided much information to work with, so this is a shot in the dark.
Why don't you try this:
If the id is numeric by definition, enforce it by removing everything else using a regular expression.
(As jproffit wrote in his comment to your question: more info on function_to_get_id_number() would be nice. Why do you need to buffer the output in the first place? Can't function_to_... return a proper value instead of outputting a string?)

PHP doesn't detect white space in string

I'm working on transferring data from one database to another. For this I have to map some values (string) to integers and this is where I run into a strange problem.
The string looks like this $string = "word anotherword"; so two words (or one space).
When I explode the string or count the amount of spaces it misses the white space. Why? I var_dumped the variable and it says it's a string.
Below is the code i'm using.
echo "<strong>Phases</strong>: ".$fases = mapPhase($lijst[DB_PREFIX.'projectPhase']);
The string that's being send to the function is for example "Design Concept". This calls the following function (where the spaces get ignored)
function mapPhase($phases){
echo "Whitespace amount: ".substr_count($phases, ' ')."<br />";
}
For the example string given this function echoes 0. What's causing this and how can i fix it? The strangest thing is that for one instance the function worked perfectly.
More than one whitespaces (in HTML) are always converter into one whitespace. For example code indents.
If you want to print more than one, one by one use &nbps; instead.
function mapPhase($phases){
echo 'Whitespace amount: '.substr_count($phases, ' ').'<br />';
}
It may well be that the alleged space in the string may not be a space as in ' ', but something similar, which gets rendered in the browser in the same way as ' ' would. (for a rudimentary list of possible characters: http://php.net/manual/en/function.trim.php)
Thus, checking what the whitespace exactly is may be the solution to that problem.
Maybe they are not even spaces. Try ord() for each symbol in your string.
ord(' ') is 32.
You can use:
$string = preg_replace('/\s+/', '', $string);

Replace newline from MySQL TEXT field to parse w/ JSON

"replace newline" seems to be a question asked here and there like hundred times already. But however, i haven't found any working solution for myself yet.
I have a textarea that i use to save data into DB. Then using AJAX I want to get data from the DB in the backend that is in TEXT field and to pass it to frontend using JSON. But pasing JSON returns an error, as new lines from DB are not valid JSON syntax, I guess i should use \n instead...
But how do i replace newlinew from DB with \n?
I've tried this
$t = str_replace('<br />', '\n', nl2br($t));
and this
$t = preg_replace("/\r\n|\n\r|\r|\n/", "\n", $t);
and using CHAR(13) and CHAR(10), and still I get an error
the new line in textarea is equivalent to, i guess
$t = 'text with a
newline';
it gives the same error. And in notepad i clearly see that it is crlf
You need to escape all the characters that have a special meaning in JSON, not only line feeds. And you also need to convert to UTF-8.
There's no need to reinvent the wheel, json_encode() can do everything for you.
Prfff... >_< silly me
I've lost another slash before replacing with \n
$t = preg_replace("/\r\n|\n\r|\r|\n/", "\\n", $t);

Categories