php - Extract keywords/tags from string using Preg_match_all -


I have the following code

  $ str = "keyword keyword 'keyword1 and keyword 2 'one more' one more '. Another keyword "yes, one, two'; Preg_match_all ('/ "[^"] + "| [^" \',] + | \ '[^ \'] + \ '/', $ str, $ matches); Echo "& lt; east & gt;"; Print_r (matches $); Echo "& lt; / pre & gt;";  

Where I want it to remove the keyword from the string, and keep wrapped in one or double quotation marks, the code above works fine, but it does not have the value quotation marks Gives along I know that I can extract them through str_replace or similar, but I'm actually looking for a way to solve it through the preg_match_all function.

Output:

  Array ([0] => array ([0] = & gt; keyword [1] = & gt; keyword [2] = [4] = & gt; one more '[5] = & gt; "second keyword" [6] => Yes [7] = & Gt; one [8] = & gt; two)) Besides, I think my Regex is a little bit, so any suggestions for better would be good :) < / P> 

Any suggestions / help would be greatly appreciated.

You almost got it; You only need to use forms to match quotation marks:

  '/ (? & Lt; = \') [^ \ '\ s] [^ \'] * + (? = \) | (? & Lt; = ") [^" \ s] [^ "] * + (? =") | [^ \ '", \ S] + /'  

Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -