get rid of " within ""

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
joachimseitz
Forum Commoner
Posts: 25
Joined: Fri Feb 20, 2004 10:36 am
Location: Germany
Contact:

get rid of " within ""

Post by joachimseitz »

Ok, my problem is this.
I have a file with this text: (i save the file as a string in php with file_get_contents())

Code: Select all

 
hello i am text="hello"
how are you doing text="buuhu"
what text="sefdsf"dfsfsdf"
sdfsdfd text=""freak" sdf"
 
what I want to do is get rid of all the " within text="xxxxxxxxx"
so text="xxx"xx" --> text="xxxxxx" (in example line 4 and 5 are bad)
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: get rid of " within ""

Post by omniuni »

Hello, try this:

Code: Select all

 
function removeQuotes($string){
$noQuotes = str_replace('"','', $string);
return $noQuotes
}
 
//to call the function, on a string $iHaveQuotes
 
$iHaveQuotes = 'I Have """ Quotes!!!';
$iHaveQuotes = removeQuotes($iHaveQuotes);
 
//$iHaveQuotes now is equal to 'I Have  Quotes!!!'
 
 
joachimseitz
Forum Commoner
Posts: 25
Joined: Fri Feb 20, 2004 10:36 am
Location: Germany
Contact:

Re: get rid of " within ""

Post by joachimseitz »

Thank you for the reply.

The problem is I want keep text="xxxxxx" those quotes but only want to delete the quotes within text="within".

so the output of my top example would be:

Code: Select all

# hello i am text="hello"
# how are you doing text="buuhu"
# what text="sefdsfdfsfsdf"
# sdfsdfd text="freak sdf"
any human I till to do that will do it but how to programm it :/
basicly the function can remove all except a-z und A-Z and space within text="within"
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: get rid of " within ""

Post by prometheuzz »

Code: Select all

echo preg_replace('/(?<!=)"(?=[^"]*")/', '', 'sdfsdfd text=""freak" sdf"');
joachimseitz
Forum Commoner
Posts: 25
Joined: Fri Feb 20, 2004 10:36 am
Location: Germany
Contact:

Re: get rid of " within ""

Post by joachimseitz »

gives me this error: Parse error: syntax error, unexpected '*' in .../quotes.php on line 7
i expect:
sdfsdfd text="freak sdf"
will be the result

but I need the whole text to be searched and the replace be done. and the stuff within text="asfsdfs" is always different

Code: Select all

 
$text='hello i am text="hello"
how are you doing text="buuhu"
what text="sefdsf"dfsfsdf"
sdfsdfd text=""freak" sdf" ';
 
so i need a function like:

Code: Select all

 
$text="hello i am text="hello"
how are you doing text="buuhu"
what text="sefdsf"dfsfsdf"
sdfsdfd text=""freak" sdf"";
 
$new_text=replace_quotes((look for " within text="") ,'', $text);
echo $new_text;
//--> results -->
//hello i am text="hello"
//how are you doing text="buuhu"
//what text="sefdsfdfsfsdf"
//sdfsdfd text="freak sdf"
 
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: get rid of " within ""

Post by prometheuzz »

joachimseitz wrote:gives me this error: Parse error: syntax error, unexpected '*' in .../quotes.php on line 7
i expect:
sdfsdfd text="freak sdf"
will be the result
Well, that is the result. Create a file with only that single line I posted, and you'll see that it is correct. You apparently didn't implement my suggestion properly.
joachimseitz wrote:but I need the whole text to be searched ...
First get that single line I posted working. Then (try) to understand how (and why) it works. Finally expand on that to do all the replacements in your text.
joachimseitz
Forum Commoner
Posts: 25
Joined: Fri Feb 20, 2004 10:36 am
Location: Germany
Contact:

Re: get rid of " within ""

Post by joachimseitz »

Yes I have gotten it to work now. I don't actually know why it didn't work the first time. I did the same thing, copy and past in a file :/ (and yes its line seven because i had some old variables on the top but they are there now to and it works now anyways weird)

I have never needed more complicated regex yet.

I am using: http://www.regular-expressions.info/reference.html to understand it. I tryed fixing yours, but since it was already correct I guess thats why I didn't find an error.

I will see if I can figure it out. And ask again if I need more help.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: get rid of " within ""

Post by prometheuzz »

joachimseitz wrote:...

I will see if I can figure it out. And ask again if I need more help.
No problem.
joachimseitz
Forum Commoner
Posts: 25
Joined: Fri Feb 20, 2004 10:36 am
Location: Germany
Contact:

Re: get rid of " within ""

Post by joachimseitz »

Ok through testing and partially understanding I have a solution.
Probably unperfect, but it works for me.

Code: Select all

$text='hello i am text="hello"
how are you doing text="buuhu"
what text="sefdsf"dfsfsdf"
sdfsdfd text=""freak" sdf"';
 
$text= preg_replace('/(?<!=)"(?![^"]*=)/', 'QUO_TES', $text);
 
//from http://www.webmasterworld.com/php/3853648.htm (third post)
$text = preg_replace('/QUO_TES(?![^QUO_TES]+QUO_TES)/','"',$text); 
//$text= preg_replace('/QUO_TES/', '', $text); im guessing str_replace is faster but both work
$text= str_replace('QUO_TES', '', $text);
echo $text;
//hello i am text="hello" how are you doing text="buuhu" what text="sefdsfdfsfsdf" sdfsdfd text="freak sdf"
 
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: get rid of " within ""

Post by prometheuzz »

joachimseitz wrote:Ok through testing and partially understanding I have a solution.
Probably unperfect, but it works for me.
...
Note that it also replaces the very last quote, something that should not happen I think. If that is correct, I suggest this minor tweak:

Code: Select all

$text= preg_replace('/(?<!=)"(?=[^=]*")/', 'QUO_TES', $text);
But you were very close: well done!
joachimseitz
Forum Commoner
Posts: 25
Joined: Fri Feb 20, 2004 10:36 am
Location: Germany
Contact:

Re: get rid of " within ""

Post by joachimseitz »

Note that it also replaces the very last quote
Yes thats correct, thats why I replaced the last QUO_TES with " again.
But your solution is better and makes more sense ;), Thx.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: get rid of " within ""

Post by prometheuzz »

joachimseitz wrote:
Note that it also replaces the very last quote
Yes thats correct, thats why I replaced the last QUO_TES with " again.
Ah yes, I didn't even look at your code: I only glanced over your regex.
joachimseitz wrote:But your solution is better and makes more sense ;), Thx.
You're welcome.
Post Reply