regex :(

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

regex :(

Post by mudkicker »

hey people,

i have a biiiiiiiiiiiiig problem and don't know how can i get through this.. i read since 2 hours regec documentaiton, tutorials. etc.. but i still can't get an easy code...

so if you can help me it will be really appreciated..

what i want to do is

i have a big text and in there i got this

Code: Select all

<kod>here is some codes for ex. <? echo 'hi'; ?> but some other too.. </kod>
we change this line (like phpbb) does to a source..

so what kinda regex should i use? :(
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

1st you need to see if

Code: Select all

exists

Code: Select all

if(ereg(\\"[kod]\\", $string) == true && ereg(\\"[/kod]\\", $string) == true)
{
$string = ereg_replace(\\"[kod]\\", \\"<kod>\\", $string);
$string = ereg_replace(\\"[/kod]\\", </kod>\\", $tring);
}
Hopefully that helps
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

well, thanks but i want to do so:
there will be a php code snippet between <kod> and </kod>..
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

Code: Select all

if(ereg("<?", $string) == true && ereg("?>", $string) == true)
{
$string = ereg_replace("<?", "<td class=kod>", $string);
$string = ereg_replace("?>", </td>", $tring);
}
youlll need to make a class in css for the code section
but that basically how you can do it
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

thank you

but how should i do if i want that code to be highlighted.

like that :

Code: Select all

<?
echo 'i''m highlighted!';
?>
thank you for your help.
Last edited by mudkicker on Tue Apr 20, 2004 12:43 pm, edited 1 time in total.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

do that in a css class!
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

doesn't it be done with highlight_string function?????
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

Code: Select all

<?php

function text_edit($text) {
	// <KOD> EDIT
	$text = str_replace("[kod]","<table width="90%" border="0"><tr><td class="kod">",$text);
	$text = str_replace("[/kod]","</td></tr></table>",$text);
	
	// <URL> EDIT
	$url = str_replace('{URL}', '\\1', '<a href="{URL}" target="_blank" class="infokonusiyah">{DESCRIPTION}</a>');
	$url = str_replace('{DESCRIPTION}', '\\2', $url);
	$pattern = "#\[url=([\w]+?://[^ "\n\r\t<]*?)\](.*?)\[/url\]#is";
	
	$text = preg_replace($pattern, $url, $text);
	
	// <PHP> EDIT
	$pattern = "#\[php\](.*?)\[/php\]#is";
}

?>
well, i came to this stage but now it's the toughest part of this function. how to take the code between <php> </php> and highlight it?

it must be done sure with highlight_string function but how?
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

[syntax=php]<?

function text_edit($text) {
// <KOD> EDIT
$text = str_replace("[kod]","<table width=\"90%\" border=\"0\"><tr><td class=\"kod\">",$text);
$text = str_replace("[/kod]","</td></tr></table>",$text);

// <URL> EDIT
$url = str_replace('{URL}', '\\1', '<a href="{URL}" target="_blank" class="infokonusiyah">{DESCRIPTION}</a>');
$url = str_replace('{DESCRIPTION}', '\\2', $url);
$pattern = "#\[url=([\w]+?://[^ \"\n\r\t<]*?)\](.*?)\[/url\]#is";

$text = preg_replace($pattern, $url, $text);

// <PHP> EDIT
$phppattern = "#\[php\](.*?)\[/php\]#is";
$phpreplace = "<? \\1 ?>";

$text = preg_replace($phppattern, $phpreplace, $text);

$text = highlight_string($text);

return $text;
}


echo text_edit("DEneme 1-2[/syntax][syntax=php]<? echo 'arif'; ?>[/syntax] merhaba bir iki [url=http://www.php.net]php[/url] [kod]#fak yoo[/kod]");
?>

here's the problem. i did that and if want to test it it only highlight the php part but doesn't replace url's..
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

well, i kinda get with this good but i have some weird problem now.

Code: Select all

<?php

function text_edit($text) {
	// <KOD> EDIT
	$text = str_replace("[kod]","<table width="90%" border="0"><tr><td class="kod">",$text);
	$text = str_replace("[/kod]","</td></tr></table>",$text);
	
	// <URL> EDIT
	$pattern = "#\[url=([\w]+?://[^ "\n\r\t<]*?)\](.*?)\[/url\]#is";
	$text = preg_replace($pattern, '<a href="\\1" target="_blank" class="infokonusiyah">\\2</a>', $text);
	
	// <PHP> EDIT
	/*$phppattern = "#\[php\]<\? (.*?) \?>\[/php\]#e";*/
	$phppattern = "/<\? (.*?) \?>/e";
	$phpreplace = "highlight_string("<? $1 ?>")";
	$text = preg_replace($phppattern, $phpreplace, $text);
	
	return $text;
}



echo text_edit('Deneme 1-2 ');
echo text_edit('<? echo "arif"; ?>');
echo text_edit('<? mysql_query("SELECT * FROM haberler"); ?>');
echo text_edit('merhaba bir iki [url=http://www.php.net]php[/url][kod]#fak yoo[/kod]');
?>
this is the code i wrote.

and the output is :

Code: Select all

Deneme 1-2 &lt;? echo "arif"; ?&gt; 1 &lt;? mysql_query("SELECT * FROM haberler"); ?&gt; 1merhaba bir iki php
#fak yoo
where did that "1" 's come out? i can't understand it? :) can you :)
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

well i think no one answers me but i get one more problem too :)

if write the php code with newlines then this code blows again!..

oh my god i'm gonna die..
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Some hints that might be interesting.

Code: Select all

<form method="post">
<textarea name="code"></textarea>
<input type="submit" />
</form>
<?php
    if (!empty($_POST['code'])) {
        /*
        *   If you change the below 1 to 0, you wont
        *   be needing the echo $tmp; part, as it's printed instantly.
        */
        $tmp = highlight_string($_POST['code'],1);
        echo $tmp;
    }
?>
User avatar
EvilWalrus
Site Admin
Posts: 209
Joined: Thu Apr 18, 2002 3:21 pm
Location: Springmont, PA USA

Post by EvilWalrus »

If you were paying attention to what people told you to do, you'd be done and over it by now.

Code: Select all

<?php
$phpreplace = "highlight_string("<? $1 ?>")"; 
?>
that code is WAYYYY wrong... highlight_string is a function, and unless you're going to eval() the code (which you're not), you need to fix that line.
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

ok people after some searching, i found some tutorials and codes and here it is! :)

btw, thank evilwalrus,jam and all people this one saved my life! ;)

here it is

Code: Select all

<?
function bbencode_highlight_php($text) {
	
	$matches = array();
	$match_count = preg_match_all("#\[php\](.*?)\[/php\]#si", $text, $matches);
	
	for ($i = 0; $i < $match_count; $i++)
	{
		$before_replace = $matches[1][$i];
		$after_replace = trim($matches[1][$i]);
		$str_to_match = "

Code: Select all

" . $before_replace . "
";
$replacement = "";
$after_replace = str_replace('<', '<', $after_replace);
$after_replace = str_replace('>', '>', $after_replace);
$after_replace = str_replace('&', '&', $after_replace);
$added = FALSE;
if (preg_match('/^<\?.*?\?>$/si', $after_replace) <= 0) {
$after_replace = "<?php $after_replace ?>";
$added = TRUE;
}
if(strcmp('4.2.0', phpversion()) > 0) {
ob_start();
highlight_string($after_replace);
$after_replace = ob_get_contents();
ob_end_clean();
}
else {
$after_replace = highlight_string($after_replace, TRUE);
}
if ($added == TRUE) {
$after_replace = str_replace('<font color="#0000BB"><?php <br>', '<font color="#0000BB">', $after_replace);
$after_replace = str_replace('<font color="#0000BB"><br>?></font>', '', $after_replace);
}
$after_replace = preg_replace('/<font color="(.*?)">/si', '<span style="color: \\1;" class="kod">', $after_replace);
$after_replace = str_replace('</font>', '</span>', $after_replace);
$after_replace = str_replace("", '', $after_replace);
$replacement .= '<table width="90%" border="0" align="center"><tr><td class="kod">'.$after_replace.'</td></tr></table>';

$text = str_replace($str_to_match, $replacement, $text);
}

return $text;
}

function text_edit($text) {
// <KOD> EDIT
$text = str_replace("[kod]","<br><table width=\"90%\" border=\"0\" align=\"center\"><tr><td class=\"kod\">",$text);
$text = str_replace("[/kod]","</td></tr></table><br>",$text);

// <URL> EDIT
$pattern = "#\[url=([\w]+?://[^ \"\n\r\t<]*?)\](.*?)\[/url\]#is";
$text = preg_replace($pattern, '<a href="$1" target="_blank" class="infokonusiyah">$2</a>', $text);
$text = bbencode_highlight_php($text);
return ($text);
}
?>
Post Reply