breakerjump Posted April 16, 2007 Share Posted April 16, 2007 Alright, so this is melting my brain... I have a string, let's say: "Here Are Some Words" and here are some more "And Yet More" How in the hell do I go about ripping whatever is inside those quotes into variables, such that: Variable One = Here Are Some Words Variable Two = And Yet More Quote Link to comment Share on other sites More sharing options...
Guest prozente Posted April 16, 2007 Share Posted April 16, 2007 <pre><?php $data = '"Here Are Some Words" and here are some more "And Yet More"'; preg_match_all('/"(.*)"/U', $data, $matches); print_r ($matches[1]); ?></pre> Would return Array ( [0] => Here Are Some Words [1] => And Yet More ) Quote Link to comment Share on other sites More sharing options...
c4onastick Posted April 16, 2007 Share Posted April 16, 2007 Ha! Interesting, I'd never seen the 'U' modifier before. It "ungreedifies" quantifiers. Well, you learn something new everyday! Quote Link to comment Share on other sites More sharing options...
junkcode Posted April 27, 2007 Share Posted April 27, 2007 I don't mean to hijack the thread but I’m looking for something like this, the above code gets me about half way to what I’m trying to achieve but I’m struggling to get it how I want/need it. $keywords = explode(" ",$_POST['keywords']); I’m currently using the above code to split keywords up into a array but I would like to keep any string in quotes together, like the above example... but the above example seems to omit the text not in quotes? My current code does this when i input a string like "one two" three four "five six" : Array ( [0] => \"one [1] => two\" [2] => three [3] => four [4] => \"five [5] => six\" ) I want to get it like this: Array ( [0] => one two [1] => three [2] => four [3] => five six ) Again sorry if I’m hijacking. Quote Link to comment Share on other sites More sharing options...
junkcode Posted April 27, 2007 Share Posted April 27, 2007 Ok after some thought I came up with the following, which does what I want it to do. There's probably an easier way to do it and my coding is a bit "down and dirty" but it gets the job done. <pre><?php $string = '"Here Are Some Words" and here are some more "And Yet More"'; preg_match_all('/"(.*)"/U', $string, $matches); // Remove the quoted text from the string. foreach ($matches[0] as &$value) { $string = str_replace($value,"",$string); } // Remove annoying white spaces. $string = trim($string); // Build the array from the results. $keywords = array_merge($matches[1], explode(" ",$string)); print_r($keywords); ?></pre> Output: Array ( [0] => Here Are Some Words [1] => And Yet More [2] => and [3] => here [4] => are [5] => some [6] => more ) Comments welcome. Quote Link to comment Share on other sites More sharing options...
effigy Posted April 27, 2007 Share Posted April 27, 2007 <pre> <?php $string = '"Here Are Some Words" and here are some more "And Yet More"'; print_r(preg_split('/"(.+?)"| /', $string, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY)); ?> </pre> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.