How to get text without some word (an ampersand issue)
I have a string like this: Hello @"user name". Where are you from, @"user name"? I开发者_JS百科 need to get the string between the " statements (user name), but I don't know how to do it.
I tried something like this /@("(.*)"|(.[^ ]*))\s*/
but it works wrong
First off, one possible regular expression that grabs the data you need is @"(.+?)"
, which matches any data within quotes preceded by @
, and captures the data inside. Now that you've added the regex you've tried, I'm betting that the issue is that your expression is greedy: the regex engine tries to grab the longest match possible, so returns all of @"user name". Where are you from, @"user name"
. Adding the ?
makes the expression lazy, so it will grab the shorter match.
Since you're interested in the content inside, I'm guessing that your final goal is to replace those strings with various types of user data dynamically, so one approach would be preg_replace_callback
:
function user_data($matches) {
$key = $matches[1];
// return the user data for a $key like "user name"
}
$output = preg_replace_callback('/@"(.+?)"/', 'user_data', $input);
try looking at this: http://www.php.net/manual/en/function.strstr.php you might need to explode the white space after and get the first item from the array as well.
If there is only one @"..."
per string, something like this should work
$matches = array();
preg_match("/@\"(.+?)\"/i", $inputstring, $matches);
echo($matches[1]);
Try this, if its not working, just escape " in pattern
/\@\"e\;([\w\s]{0,})\"e\;/
精彩评论