waynew Posted September 29, 2008 Share Posted September 29, 2008 Need some help on this one. Say for example that I have: edede deededed <code> kejhkjdkejdkejkde</code> ededed (bad example I know.) How would I take out everything inside the code tags? Quote Link to comment https://forums.phpfreaks.com/topic/126257-hey/ Share on other sites More sharing options...
Adam Posted September 29, 2008 Share Posted September 29, 2008 Erm, very vague. What kind of code is it? If you mean return everything from within two tags, say a div element(?) you could use JavaScript with something like: var content = document.getElementById('yourElementsID').innerHTML; ... (or ".value" depending on the element, like an 'input' for example) ------------ To return the JS code I have no idea, don't know if you can? Need bit more information really... Adam Quote Link to comment https://forums.phpfreaks.com/topic/126257-hey/#findComment-652853 Share on other sites More sharing options...
waynew Posted September 29, 2008 Author Share Posted September 29, 2008 Sorry, it was early in the morning. You see, I'll be re-doing my website soon and I want to format my posted PHP code a lot better. What I wish to do is get any code that is between a <code> or tag and send it through: [code=php:0] $string = highlight_string($string); Only anything that is between the <code> tags should be sent through that function however, otherwise everything will look like code. Obviously this is a job for some sort of string function where I can take out a specific part and then highlight it before putting it back.[/code] Quote Link to comment https://forums.phpfreaks.com/topic/126257-hey/#findComment-652919 Share on other sites More sharing options...
Fadion Posted September 29, 2008 Share Posted September 29, 2008 You could do this with normal string manipulation or regular expressions. The former is simpler and faster, but surely lacks the power of regular expressions and most importantly, it would be really difficult to parse the text if it has several code tags. I'm not a regular expression guy, but from my humble knowledge I came up with this: <?php $str = "here is some code: [ code ]put some code here[ /code ]"; $pattern = "/\[code\](.*?)\[\/code\]/is"; preg_match($pattern, $str, $matches); echo $matches[1]; ?> Basically the regular expression searches for a pattern of any text which is enclosed in code tags, doesn't care about case (meaning case insensitive) and takes care of newlines too. Hope this helps. EDIT: Added some space in the code tags of the string in the code i suggested, because they were interpreted as forum code tags. Just remove them when you will test the code. Quote Link to comment https://forums.phpfreaks.com/topic/126257-hey/#findComment-652930 Share on other sites More sharing options...
waynew Posted September 29, 2008 Author Share Posted September 29, 2008 GuiltyGear. My many thanks! I've been wondering about this for a while but never needed to use it, so it lay there waiting for me. Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/126257-hey/#findComment-653423 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.