Isolating all words started with %

Hello

Let’s say I have a body of text saying:

Hello %Jacob. I just went to see %Charlie today.

How do I get php to detect all the words starting with % and creating an sql query using it

for e.g
if text contains ‘%word’
{
insert into db values ‘%word’
}

Use the following pattern to match the correct words.

$s = "Hello %Jacob. I just went to see %Charlie today.";
$pattern = '#(\\B%\\w+\\b(?!%))#i';
if (preg_match_all($pattern, $s, $m))
{
        print_r($m[1]);
}

Thank you for your response. I noticed that the output is:
'Array ( [0] => %Jacob [1] => %Charlie ) ’

How would I process this data, so I can use it in a mySql query?

You can use foreach to loop the data:


$s = "Hello %Jacob. I just went to see %Charlie today.";
$pattern = '#(\\B%\\w+\\b(?!%))#i';
if (preg_match_all($pattern, $s, $m)) {
	foreach($m[1] as $name) {
		$name = substr($name, 1);
		$sql = 'INSERT INTO table VALUES("'. $name . '")';
		// INSERT INTO table VALUES("Jacob")
		// INSERT INTO table VALUES("Charlie") 
	}
}