Phalynx Posted September 13, 2011 Share Posted September 13, 2011 What is the correct code to accomplish a search & replace with specific parameters? I have for example this: [TD=class: myclass]Dynamic String, can contain all chars, simple? [TD=class: myclass]Another Dynamic String, yup! I want this: [TD="class: myclass"]Dynamic String, can contain all chars, simple? [TD="class: myclass"]Another Dynamic String, yup! So just remove the URL tag BUT only if prefixed with the [td BBCode I tried already these pattern with preg_match_all, but failed miserably: '/\[TD="class: myclass"\]\[url="http:\/\/my.com\/static/<.*>/' Does anyone has a solution? Thanks in advance... Quote Link to comment https://forums.phpfreaks.com/topic/247082-search-and-replace-but-only-if-another-string-is-present/ Share on other sites More sharing options...
xyph Posted September 13, 2011 Share Posted September 13, 2011 I doubt this can be solved with a single RegEx. This requires parsing of values RegEx returns, probably with more RegEx. Quote Link to comment https://forums.phpfreaks.com/topic/247082-search-and-replace-but-only-if-another-string-is-present/#findComment-1268939 Share on other sites More sharing options...
requinix Posted September 13, 2011 Share Posted September 13, 2011 You can include the TD in the search text so long as you remember to put it back when you're done. // v------1------v v-2-v preg_replace('/(\[TD=[^\]]+\])\[uR L=[^\]]+\](.*?)\[\/URL\]/i', '$1$2', $string) (Don't forget to remove the space from the "UR L") Quote Link to comment https://forums.phpfreaks.com/topic/247082-search-and-replace-but-only-if-another-string-is-present/#findComment-1268982 Share on other sites More sharing options...
xyph Posted September 13, 2011 Share Posted September 13, 2011 BBCode shoudn't be parsed in code/php tags Many people like to use [url=http://google.com]this[/url] Quote Link to comment https://forums.phpfreaks.com/topic/247082-search-and-replace-but-only-if-another-string-is-present/#findComment-1268986 Share on other sites More sharing options...
requinix Posted September 13, 2011 Share Posted September 13, 2011 BBCode shoudn't be parsed in code/php tags Many people like to use [url=http://google.com]this[/url] It doesn't get translated to HTML, but the forum does add a http:// prefix. After copy/pasting what I typed and removing the space, preg_replace('/(\[TD=[^\]]+\])\[url=http://[^\]]+\](.*?)\[\/URL\]/i', '$1$2', $string) Now as it turns out, that's not actually harmful in this case, but still... Quote Link to comment https://forums.phpfreaks.com/topic/247082-search-and-replace-but-only-if-another-string-is-present/#findComment-1268993 Share on other sites More sharing options...
xyph Posted September 13, 2011 Share Posted September 13, 2011 Interesting! Odd behavior, good to know! Quote Link to comment https://forums.phpfreaks.com/topic/247082-search-and-replace-but-only-if-another-string-is-present/#findComment-1268999 Share on other sites More sharing options...
Phalynx Posted September 14, 2011 Author Share Posted September 14, 2011 Great, it works! Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/247082-search-and-replace-but-only-if-another-string-is-present/#findComment-1269350 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.