Jump to content

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/126257-hey/#findComment-652853
Share on other sites

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]

Link to comment
https://forums.phpfreaks.com/topic/126257-hey/#findComment-652919
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/126257-hey/#findComment-652930
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.