Php/Regex get the contents between a set of double quotes
Posted
by
Davy Arnold
on Stack Overflow
See other posts from Stack Overflow
or by Davy Arnold
Published on 2011-08-23T07:32:41Z
Indexed on
2012/04/13
11:28 UTC
Read the original article
Hit count: 273
Update to my question: My goal overall is to split the string into 4 parts that I can access later.
- value
- =
- "
- result of the html inside the first and last " "
Here is an example of what i'm trying to do:
// My string (this is dynamic and will change, this is just an example)
$string = 'value="<p>Some text</p> <a href="#">linky</a>"';
// Run the match and spit out the results
preg_match_all('/([^"]*)(?:\s*=\s*(\042|\047))([^"]*)/is', $string , $results);
// Here is the array I want to end up with
Array
(
[0] => Array
(
[0] => value="<p>Some text</p><a href="#">linky</a>"
)
[1] => Array
(
[0] => value
)
[2] => Array
(
[0] => "
)
[3] => Array
(
[0] => <p>Some text</p><a href="#">linky</a>
)
)
Basically the double quotes on the link are causing me some trouble so my first though was to do [^"]$ or something to have it just run until the last double quote, but that isn't getting me anywhere. Another idea I had was maybe process the string in PHP to strip out any inner quotes, but i'm not sure ho to go about this either.
Hopefully I'm being clear, it is pretty late and i've been at this far too long!
© Stack Overflow or respective owner