Solved

PHP Regex - stripping quotes

Posted on 2013-05-15
3
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 dynamically set the form action using jQuery.

752 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