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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.