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? Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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; ?> Quote Link to comment 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.