Solved

PHP Regex - stripping quotes

Posted on 2013-05-15
3
301 Views
Last Modified: 2013-05-15
Using something like preg_replace, it is possible strip quotes from both end of a string, but only if both quotes exists?

So,
$eg1 = "\"hello\""; results in hello
$eg2 = "goodbye\"" results in goodbye"
$eg3 = "yoo" results in yoo
$eg4 = "\"foo" results in "foo

Thanks
BT
0
Comment
Question by:brothertom
3 Comments
 
LVL 4

Expert Comment

by:Finec
ID: 39167591
Hello,

Try to replace this with $1:
"([^"]*)"

Open in new window

If you don't allow spaces, use this:
"([^" ]*)"

Open in new window

0
 
LVL 22

Accepted Solution

by:
Ivo Stoykov earned 500 total points
ID: 39167620
try something like
$a =   "\"foo" ;
echo preg_replace("/^[\\\"]+(.+)[\\\"]$/", '${1}', $a);

Open in new window

HTH

Ivo Stoykov
0
 

Author Closing Comment

by:brothertom
ID: 39167640
Great, thanks very much...
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question