Welcome Guest, Not a member yet? Register   Sign In
I hate Regex
#13

[eluser]TheFuzzy0ne[/eluser]
preg_match() does exactly this. http://uk.php.net/manual/en/function.preg-match.php

There are other functions that server a similar function, such as eregi(), and ereg(). Use brackets to capture the text you want.

If you're serious about learning Regular Expressions, you can check out the great tutorials [url="http://www.regular-expressions.info/"]here[/url]. Just bear in mind that there are different types of Regular Expressions that use different syntaxes, for example, preg_match() uses Perl regular Expressions.

Code:
<?php
$string = 'this is a random string of text';
$pattern = '/random ([a-z]+) of/i'; // Assuming that what we want to capture is a word containing only letters.
$results = array();
preg_match($pattern, $string, $results);
print_r($results);

/*
Results:
Array
(
    [0] => random string of // This is the entire matched string.
    [1] => string // This is the match that was "captured" using the brackets.
)

*/
?>
Hope this helps.


Messages In This Thread
I hate Regex - by El Forum - 01-08-2009, 10:49 AM
I hate Regex - by El Forum - 01-08-2009, 11:03 AM
I hate Regex - by El Forum - 01-08-2009, 11:03 AM
I hate Regex - by El Forum - 01-08-2009, 11:24 AM
I hate Regex - by El Forum - 01-08-2009, 11:27 AM
I hate Regex - by El Forum - 01-08-2009, 11:35 AM
I hate Regex - by El Forum - 01-08-2009, 11:36 AM
I hate Regex - by El Forum - 01-08-2009, 11:41 AM
I hate Regex - by El Forum - 01-08-2009, 12:12 PM
I hate Regex - by El Forum - 01-08-2009, 01:26 PM
I hate Regex - by El Forum - 01-08-2009, 02:42 PM
I hate Regex - by El Forum - 01-08-2009, 02:50 PM
I hate Regex - by El Forum - 01-08-2009, 03:07 PM
I hate Regex - by El Forum - 01-08-2009, 03:20 PM



Theme © iAndrew 2016 - Forum software by © MyBB