How can I replace apostrophes while uploading a csv file? - php

I'm having trouble replacing apostrophes while uploading a csv file with a bunch of different descriptions.
Right now I have
$remarks = str_replace("'", "’", $data[28]);
This gives me an error starting with the first apostrophe that shows up in my file. That first phrase where the apostrophe appears ends in "'s". If I change it to
$remarks = str_replace("'s", "’", $data[28]);
it will go past that first problem and get to the next problem ('t).
What am I doing wrong? I'm new to php, and I'm sure this must be a simple solution...

array_map($data, function($a) { return(str_replace($a, "'", "’")) });
should walk all elements of the array and replace all quotes for you.

It looks like you're trying to re-invent the wheel. It looks like you're trying to parse the csv yourself. If you are stop it. You should be using str_getcsv and you won't have to worry about escaping anything.
After that, you'll probably want to look into preg_replace.
preg_replace( "#'\w?#g", '', $data[$index] );

Related

Regex - Replace beginning from last > to a tag

Before all: My english is not that good, so... I'd like to ask for apologizes if you guys can't understand me :)
So, this is what I'm looking for:
I'm being using a Wordpress plugin to generate XML (WP ALL EXPORT). Good.
Now, I need to open a file and edit some stuffs. I started with:
$data = file_get_contents("1439828483.xml");
And now I'm working using str_replace and preg_replace to update the lines I need.
I have two XML tag like these:
<cidade><![CDATA[sao-paulo>santo-andre]]></cidade>
<bairro><![CDATA[sao-paulo>santo-andre]]></bairro>
You see the content is the same... but it's because I have one ">" character splitting 2 stuff.
In the <cidade></cidade> tag I need to keep only what is before ">".
In the <bairro></bairro> tag I need to keep only what is after ">".
For the second problem, I fixed using this:
$data = preg_replace('#(<bairro>).*?(>)#', '$1$2', $data);
$data = str_replace('<bairro>>', "<bairro><![CDATA[",$data);
The result is:
<bairro><![CDATA[santo-andre]]></bairro>
OK, I have the content but it still have hyphens (dashes) and now I'm not able to fix it (No idea how to). What I really need is:
<bairro><![CDATA[santo andre]]></bairro>
And of course, for the tag <cidade></cidade> I would need to have:
<cidade><![CDATA[sao paulo]]></cidade>
Before posting here, I found this topic:
Regex between, from the last to specific end
But I tried to edit some parts of anubhava and Jack Maney answers but I failed :(
As I'm using preg_replace and str_replace I don't know if there is some limitations for regex strings.
Thanks and I hope you guys can understand me :D
This will do it (and replaces your own fix):
$data = preg_replace('#(<bairro><!\[CDATA\[)[^>]*?>([^>]*?><)#', '$1$2', $data);
while(preg_match('#(<bairro>[^->]*?)-([^->]*?-)*([^->]*?'.'>)#', $data)) {
$data = preg_replace('#(<bairro>[^->]*?)-(([^->]*?-)*)([^->]*?'.'>)#', '$1 $2$4', $data);
}
$data = preg_replace('#(<cidade><!\[CDATA\[[^>]*?)>[^>]*?(\]\]><)#', '$1$2', $data);
while(preg_match('#(<cidade>[^->]*?)-([^->]*?-)*([^->]*?'.'>)#', $data)) {
$data = preg_replace('#(<cidade>[^->]*?)-(([^->]*?-)*)([^->]*?'.'>)#', '$1 $2$4', $data);
}
Let me just point out that parsing XML with regex is often a bad idea, partly for reasons you're discovering. However, if all you want to do is replace hyphens with spaces, just do this:
$data = str_replace_all('-', " ", $data);
This will replace ALL the hyphens in your input, of course, so make sure you know what's in there.

PHP: remove MySQL formatting such as line break and commas

I'm making a CSV file, one of the database filed has stored a section called comments/note which obviously have some commas and line breaks in it too. Looked around the web found usage of preg_replace(), not much familiar with regular expressions there fore combined two different ones and not getting anything in result its totally blank and i know all records have some sort of comments in it
this i used
preg_replace( "/\r|\n/|/[,]/", "", $string )
Please what do I need to do here get one text back without line breaks and commas
Regards
You can do it using strip_tags() and preg_replace();
$clean_str = trim(preg_replace('/\s\s+/', '', strip_tags($string)));
or try this
$clean_str = str_replace(array("\r\n","\r","\n", ","), '', strip_tags($string));

Is there a typo in this str_replace code? / Am I reading it correctly?

Here is the line of code from a PHP file, specifically it is from zstore.php which is a file include as part of the "Zazzle Store Builder" toolset from Zazzle.com
The set of files allows someone like me, who has products for sale on Zazzle and massage that data into a nicer "storefront" which I can set up my way instead of being confined by the CMS structure of Zazzle.com where they understandably want to keep the monkeys (uhmmm... users like myself) from causing too much mayhem.
So... here is the code:
$keywords = str_replace(" ",",",str_replace(",","",$keywords));
Two questions:
Am I understanding what it does and
Is there an extra single or double quote in the string that does not need to be there?
Here is what I think the line of code is saying:
Take the string of characters that the user inputs (dance diva) and assign it to the variable called
$keywords
then run the following function on that character string
= str_replace
(" ","," <<< look for spaces. If you find a space, replace it with a comma
,str_replace(",","" <<< this is the bit I don't understand or which may have a typo
I THINK that it is saying " if you find commas, leave them alone, but I'm not certain.
,$keywords)); <<< then put the edited string of characters backing to the variable called $keywords.
What lead me to look at this was that I was inputting the following:
dance,diva which is what I THOUGHT the script was wanting from me based on the commented text in the README.txt file:
// Search terms. Comma separated keywords you can use to select products for your store
So..
Am I understanding what this line of code is supposed to do?
which, assuming I am correct, and I'm pretty sure that the first half is supposed to work as I've described, now brings me to my second question:
Why isn't the second bit working? Is there a typo?
To review:
dance diva produces results
dance,diva does not
Both, SHOULD work.
Thanks in advance for your help. I have a lot of HTML experience and computer experience but PHP is new to me.
$keywords = str_replace(" ",",",str_replace(",","",$keywords));
You can split into
$temp = str_replace(",","",$keywords);
$keywords = str_replace(" ",",",$temp);
First it replaces all comas with empty string, it is removes all comas. Then replaces all spaces with comas.
For "dance diva" there are no comas so first does nothing, then it replaces space and result is "dance,diva"
For "dance,diva" it removes coma, you get "dancediva" and there in no space to replace next so it is Your result.

html source encode

when I view source on my php page I get " for a quote. But instead, I would like " to be used in the source code. I have no control over manually replacing it so Im wondering if there is a function to do such a thing.
If you have access to the PHP and want to change all html special characters to their rightful variations use:
print htmlspecialchars_decode($string);
You could do this very simply using str_replace.
$string = str_replace('"', '"', $string);
However, as Levi said, why not just leave it this way? It should have no effect on the display.

Searching through a string trying to find ' in PHP

I am using tinyMCE and, rather annoyingly, it replaces all of my apostrophes with their HTML numeric equivalent. Now most of the time this isn't a problem but for some reason I am having a problem storing the apostrophe replacement. So i have to search through the string and replace them all. Any help would be much appreciated
did you try:
$string = str_replace("'", "<replacement>", $string);
Is it just apostrophes that you want decoded from HTML entities, or everything?
print html_entity_decode("Hello, that's an apostophe.", ENT_QUOTE);
will print
Hello, that's an apostrophe.
Why work around the problem when you can fix the cause? You can just turn of the TinyMCE entity encoding*. More info: here
*Unless you want all the other characters encoded, that is.

Categories