everisk Posted October 23, 2008 Share Posted October 23, 2008 Hello, I dont understand a little about RegEx but cannot really understand how to use all the funtions . I need to convert link tag <a href ....> that DOES NOT already have the css in them. Such as <a href="http://www.google.com" style="font-size: 11px;" other properties> should NOT matched but <a href="http://www.google.com" .. other properties> should match and a style="..." should be added. I understand that I need to use preg_replace for that but dont know how to use it Any help would be much appreciated. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/129715-regex-to-add-inline-css-style-tag-to-link-tag/ Share on other sites More sharing options...
xylex Posted October 23, 2008 Share Posted October 23, 2008 Any particular reason you're trying to do this with regex and not an XML library? Quote Link to comment https://forums.phpfreaks.com/topic/129715-regex-to-add-inline-css-style-tag-to-link-tag/#findComment-672569 Share on other sites More sharing options...
discomatt Posted October 23, 2008 Share Posted October 23, 2008 A regex like this will work '/<a(?:.(?!style="))*?>/s' But keep in mind, you're using lookaround along with a lazy quantifier... this won't be the most efficient expression in the world. Quote Link to comment https://forums.phpfreaks.com/topic/129715-regex-to-add-inline-css-style-tag-to-link-tag/#findComment-673035 Share on other sites More sharing options...
everisk Posted October 24, 2008 Author Share Posted October 24, 2008 Discomatt, thank you for your help! I think i got it. Quote Link to comment https://forums.phpfreaks.com/topic/129715-regex-to-add-inline-css-style-tag-to-link-tag/#findComment-673471 Share on other sites More sharing options...
effigy Posted October 24, 2008 Share Posted October 24, 2008 <?php $data = <<<DATA I dont understand a little about RegEx but cannot really understand how to use all the funtions . I need to convert link tag <a href ....> that DOES NOT already have the css in them. Such as <a href="http://www.google.com" style="font-size: 11px;" other properties> should NOT matched but <a href="http://www.google.com" .. other properties> should match and a style="..." should be added. DATA; echo htmlspecialchars(preg_replace('/<a((??!style=)[^>])+)>/', '<a style="xyz" $1>', $data)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/129715-regex-to-add-inline-css-style-tag-to-link-tag/#findComment-673765 Share on other sites More sharing options...
everisk Posted October 29, 2008 Author Share Posted October 29, 2008 Thank you for a more complete example, effigy! I'll try that out. Quote Link to comment https://forums.phpfreaks.com/topic/129715-regex-to-add-inline-css-style-tag-to-link-tag/#findComment-677487 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.