Jump to content

need a little preg_grep / regex help


mycodetree

Recommended Posts

Question 1:

$text = array("This is a string for demonstration testing");

$result = preg_grep("/test/",$text);

 

In the above, preg_grep would hit on the text 'test', within the word 'testing'.

 

My question is, how can I instruct the PCRE (with preg_grep preferably) engine to only hit on the text 'test' when it appears as its own word, and not a series of characters within another word?

 

Question 2:

$text = array("This is a test string for demonstration testing");

$result = preg_grep("/test/",$text);

 

In the above text string, how can I instruct the PCRE (with preg_grep preferably) engine to hit on all instances of the character series 'test'? After it hits on the first character series, 'test', how can I use preg_grep to continue searching the string for other instances of 'test'?

 

Question 3:

 

Does anyone have any good Internet resources on Regex that doesn't require you to understand hieroglyphs?

 

Link to comment
https://forums.phpfreaks.com/topic/250451-need-a-little-preg_grep-regex-help/
Share on other sites

preg_grep is for matching on elements of an input array:

 

<?php
$array = array('Test', 'testing', 'test', 'fail');
$result = preg_grep('/test/', $array);
?>

 

$result will output:

Array
(
    [1] => testing
    [2] => test
)

 

Perhaps go into a bit more detail on what you are trying to accomplish with regex

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.