Forum Moderators: coopster
I'm having a lot of trouble figuring out how to search for <code>(ANTYHING EXCEPT </code>)</code>.
Here's what I think should work: <code>[^(?:<\/code>)]+<\/code>
Any suggestions on how to get this to work?
[edited by: coopster at 4:12 pm (utc) on July 29, 2006]
[edit reason]
[1][edit reason] Disable graphic smile faces [/edit] [/edit][/1]
preg_match [uk2.php.net] is ideal for grabbing data between tags:
$data = '<code>I am some code..</code>';$code = preg_match( "/<code>(.+)<\/code>/si", $data, $match);
$code = strip_tags($match[1]);echo $code;
dc
if you have it multiple times, then you need to use something like following:
/\<code\>((?:(?!\<\/code\>).)*)/
my regex is of Perl, but guessing, should work with PHP also.
do test it rigorously.
[edited by: coopster at 4:13 pm (utc) on July 29, 2006]
[edit reason]
[1][edit reason] Disable graphic smile faces [/edit] [/edit][/1]
Given the following code:
abcdefg<code>hijklmn</code>opqrstu<code>vwxy</code>z
Your regular expression returns hijklmnopqrstuvwxy
I would like it to return hijklmn and vwxy respectively
Is there anything else you can suggest? The code I am highlighting is C++, but I need to run things through my own parser because people on my site need specific highlighting that most C++ users wouldnt.
<?
$code = 'class CMatlabEng {
public:
<code>/* int OutputBuffer(char *p, int n);</code>
void OpenSingleUse(const char *startcmd, void *dcom, int *retstatus);
int GetVisible(bool* value);*/
int SetVisible(bool value); mxArray* GetVariable(const char* name);
<code> int PutVariable(const char *name, const mxArray *mp);
int EvalString(const char* string);
void Open(const char* StartCmd); int Close(); CMatlabEng();
//virtual ~CMatlabEng();</code>
protected:
Engine* pEng;
}; ';
$search = "#<code>(.+)<\/code>#si";
preg_replace_callback($search, 'fetch', str_replace(array("\n"," "," "),array("<br>"," ","  l "),$code));
function fetch($matches)
{
print_r($matches[1]);
// return parsed code (cut out for posting)
}
?>
Edit: I have decided that I could convert all < and > within the code, and then use the search string '#<code>([^<]+<\/code>#si' except that seems like it's just applying duct tape, not honestly getting the answer. And what's more, I won't have learned the solution for next time.
You have it all good to go, except you need to use the Ungreedy modifier [php.net] in your pattern:
$search = "#<code>(.+)<\/code>#Uis";
I'm no regex expert, but everything I've done all week long has involved them for some reason...
First of all, the most appropriate function in php for this task is probably preg_replace_callback() [ca3.php.net]--since it allows you to take each match and do some work on it as opposed to preg_match() and preg_match_all() which simply return one or more matches from the original. I suspect it's better for this case too since it's won't just be a simple replacement of one string with another (else we could use preg_replace()).
Having said that, I'm not quite clever enough to have done a tidy job of it--but here's what I came up with:
<?php$source = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas dictum sapien vitae neque. Maecenas dui. Vestibulum suscipit, magna ut sodales tempor, sem arcu mollis sapien, cursus mollis ipsum ipsum vel erat. Nulla tempor est at mauris. Proin ullamcorper tortor non tellus. Suspendisse pretium dui ut augue. Aliquam vitae mauris. Cras feugiat nulla a metus. Vivamus eu lectus at sem vestibulum pretium. Sed odio sapien, tempus at, venenatis id, faucibus ac, nulla. Aenean congue lacinia tortor. <code>Morbi</code> adipiscing, lorem a facilisis imperdiet, nulla ante venenatis erat, et <foo>nonummy <em>mi</em></foo> massa sit amet <code>ipsum. Cras</code> et orci. Ut rutrum ultricies sapien. Ut ut urna in mauris ornare tincidunt. Ut magna sem, iaculis condimentum, laoreet et, elementum et, mi.<p><code><em>Aenean auctor</em> placerat</code> orci.</p> Quisque blandit sapien eu nisi dictum rhoncus.';
$pattern = '/<code>(.+?)<\/code>/';
function highlight($matches) {
// Manipulate content here--$matches[1] contains the content of the <code> element:
$output = '<span style="color:red;font-weight:bold;">' . $matches[1] . '</span>';// Return the altered string:
return '<code>' . $output . '</code>';}
echo preg_replace_callback($pattern, 'highlight', $source);
?>
It seems to me that it should have been possible to do this (maybe with lookahead?) without having to re-insert the code tags. However, it does seem to work in all the limited contexts I've tried it in--leaves other tags alone etc.
-b
/\<code\>((?:(?!\<\/code\>).)*)/
(This was the code I was originally trying to get in the beginning, but of course mine was invalid)
[edited by: coopster at 9:47 pm (utc) on July 29, 2006]
[edit reason]
[1][edit reason] Disable graphic smile faces [/edit] [/edit][/1]