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

[eluser]Iverson[/eluser]
Crazy question...

I'm addicted to the substr function, but there doesn't seem to be a way to do this using a regex. I know about preg match and preg replace, bla bla bla, but this doesn't serve my purpose. I need to extract a string when I don't know what the actual string will be. I only know what the surrounding text will be.
#12

[eluser]Eric Cope[/eluser]
Would an XML parser do the trick?
#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.
#14

[eluser]Iverson[/eluser]
[quote author="Eric Cope" date="1231469450"]Would an XML parser do the trick?[/quote]

I like the way you think! I just finished putting it in somewhat of an XML format:

Code:
<title>Column Title</title>
<content>Column Content</content>

Too bad CI doesn't have a neat XML parser. I'll look into using SimpleXML.




Theme © iAndrew 2016 - Forum software by © MyBB