ShaolinF Posted August 22, 2010 Share Posted August 22, 2010 Hi Guys, I have the following URL: <a href="test.test"></a> I want to remove everything except the href="test.test" bit so I did the following: [^(href=\"(.*)\")] but this returns href="e.e". Any ideas? Link to comment https://forums.phpfreaks.com/topic/211440-help-with-regex/ Share on other sites More sharing options...
premiso Posted August 22, 2010 Share Posted August 22, 2010 Why not just pull out the url and then build your own <a href statement. This would surely be easier and then you do not have to worry about having a complex regex to attempt to do it. Link to comment https://forums.phpfreaks.com/topic/211440-help-with-regex/#findComment-1102420 Share on other sites More sharing options...
GoneNowBye Posted August 22, 2010 Share Posted August 22, 2010 [] means a class, or posible options [abc] means a or b or c so....your telling regex to match not match alot of stuff.... anyway what you want is: (href="(.*?)") that should work fine (as your entire expression, you dont need / / at the start and the end) the data between quotes will be at result_array[1] btw and the * is greedy, it matches any character AS MUCH AS IT CAN you want lazy to match it as little as posible (up until the next quote) otherwise it would match for example href="stuff" more "stuff" "doo" "foo" "bar" greedy regex matches: stuff" more "stuff" "doo" "foo" "bar Lazy matches: stuff the *? makes it 0 or more matches - but as little as posible. Link to comment https://forums.phpfreaks.com/topic/211440-help-with-regex/#findComment-1102421 Share on other sites More sharing options...
ShaolinF Posted August 22, 2010 Author Share Posted August 22, 2010 [] means a class, or posible options [abc] means a or b or c so....your telling regex to match not match alot of stuff.... anyway what you want is: (href="(.*?)") that should work fine (as your entire expression, you dont need / / at the start and the end) the data between quotes will be at result_array[1] btw and the * is greedy, it matches any character AS MUCH AS IT CAN you want lazy to match it as little as posible (up until the next quote) otherwise it would match for example href="stuff" more "stuff" "doo" "foo" "bar" greedy regex matches: stuff" more "stuff" "doo" "foo" "bar Lazy matches: stuff the *? makes it 0 or more matches - but as little as posible. Thanks - That does work but what Im trying to do is remove everything other than the href="test.htm" bit. How would I do that ? The only way I can think of doing this is using something like [^(expression here)] but that will match on a per letter basis. Link to comment https://forums.phpfreaks.com/topic/211440-help-with-regex/#findComment-1102433 Share on other sites More sharing options...
GoneNowBye Posted August 22, 2010 Share Posted August 22, 2010 check your PM i think you want PREG_REPLACE check your pm and i'll probably be able to help Link to comment https://forums.phpfreaks.com/topic/211440-help-with-regex/#findComment-1102440 Share on other sites More sharing options...
sasa Posted August 23, 2010 Share Posted August 23, 2010 <?php $test = '<a href="test.test"></a>'; $test = preg_replace('/^.*(href="[^"]*").*$/','$1',$test); Echo $test; ?> Link to comment https://forums.phpfreaks.com/topic/211440-help-with-regex/#findComment-1102606 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.