king.oslo Posted January 9, 2010 Share Posted January 9, 2010 Hello, I often find myself wanting to remove everything that does not match a pattern, or only return the matches. I.e: remove everything apart from the email address (underlined) from this string '<Marius Jackson <m.jackson@example.com>' or this: remove everything apart from phone number (underlined) from this string 'Marius Jackson, Norway, +47 412 43 120, Lokalrådgiver'. I feel it would be very easy if I could use preg_filter, but my server is not PHP 5 >= 5.3.0, because I understand that this function returns only matches. And preg_match_all returns an array of all sorts of things, and i find it uncomfortable to use. How do I do this easily? Thanks, Marius Quote Link to comment https://forums.phpfreaks.com/topic/187836-preg_replace-replace-everything-that-does-not-match/ Share on other sites More sharing options...
spfoonnewb Posted January 9, 2010 Share Posted January 9, 2010 Why not just use preg_match()? Quote Link to comment https://forums.phpfreaks.com/topic/187836-preg_replace-replace-everything-that-does-not-match/#findComment-991734 Share on other sites More sharing options...
salathe Posted January 9, 2010 Share Posted January 9, 2010 The preg_match (and preg_match_all) are designed for that sort of task. If you want a simple function to grab the matched value quickly then something like the following might work: function preg_grab($pattern, $subject, $group = 0) { if (preg_match($pattern, $subject, $match) AND isset($match[$group]) { return $match[$group]; } return FALSE; } Then you can call that in a similar way to preg_replace like: $string = 'Marius Jackson <m.jackson@example.com>'; echo preg_grab($email_pattern, $string); // m.jackson@example.com echo preg_grab('/Marius (\w+)/', $string, 1); // Jackson Quote Link to comment https://forums.phpfreaks.com/topic/187836-preg_replace-replace-everything-that-does-not-match/#findComment-991735 Share on other sites More sharing options...
king.oslo Posted January 11, 2010 Author Share Posted January 11, 2010 Hey, Thanks, That was a great function, but waht is the group argument for? Thanks, Marius Quote Link to comment https://forums.phpfreaks.com/topic/187836-preg_replace-replace-everything-that-does-not-match/#findComment-992467 Share on other sites More sharing options...
nrg_alpha Posted January 11, 2010 Share Posted January 11, 2010 The group argument supplies a default value of 0. Within the pattern, everything is stored within $match[0] (in this case anyway) by default. But I suspect if you wanted to ensure a capture (say first capture in a pattern, thus $match[1]) exists, you can specifiy this instead. Just gives you extra flexibility to check if a certain capture exists, as opposed to the entire base pattern ($match[0]). Quote Link to comment https://forums.phpfreaks.com/topic/187836-preg_replace-replace-everything-that-does-not-match/#findComment-992470 Share on other sites More sharing options...
king.oslo Posted January 11, 2010 Author Share Posted January 11, 2010 Great! Thank you for this! Kind regards, Marius Quote Link to comment https://forums.phpfreaks.com/topic/187836-preg_replace-replace-everything-that-does-not-match/#findComment-992472 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.