Page 1 of 1

Replace and Format text between tags.

Posted: Fri Aug 26, 2005 10:46 pm
by apg88
I have this function:

Code: Select all

function parseCode($input)
	{
		
		$rules = array("#\<code\>(.*?)\</code\>#si");		
		$changeto = array("<br>Code:<br>\n<div style=\"border: 1px solid black; background: #9cdff1\">\\1</div>");
		$input = preg_replace($rules,$changeto,$input);		

		return $input;
		
	}
What it does is take out the < code > tags and wrap their contents on a < div > tag.
What I want to do is get the content between the < code > tags, run in through a function, and replace the old text with it.

For example, lets say I have this string:

Code: Select all

<code>this is the text inside of code</code>
this is not
<code> and yet again, this is inside code again, again</code>
now its not
It should give out:

Code: Select all

<br>Code:<br>
<div style="border: 1px solid black; background: #9cdff1">this is the text inside of code<div>
this is not
<br>Code:<br>
<div style="border: 1px solid black; background: #9cdff1">and yet again, this is inside code again, again<div>
now its not
Now, lets say I want all of the text inside the < code > tags to run through the html2unicode($string) function before I place it back on the main string, how would I go about doing that?

Thank you very much in advance,

apg88

Posted: Fri Aug 26, 2005 10:52 pm
by feyd

Posted: Fri Aug 26, 2005 11:29 pm
by apg88
Thanks!
I found a way to do it in that manual page of the function, so i figured this would work....

Code: Select all

function parseCode($input)
	{
		
		$rules = array("#\<code\>(.*?)\</code\>#si");		
		$changeto = array("<br>Code:<br>\n<div style=\"border: 1px solid black; background: #9cdff1\">" . $this->html2unicode('\\1') . "</div>");
		$input = preg_replace($rules,$changeto,$input);		

		return $input;
		
	}
	
	function html2unicode ($input)
	{
		$input = htmlentities($input);
		$htmlEntities = array_values (get_html_translation_table (HTML_ENTITIES, ENT_QUOTES));
		$entitiesDecoded = array_keys  (get_html_translation_table (HTML_ENTITIES, ENT_QUOTES));
		$num = count ($entitiesDecoded);
		for ($u = 0; $u < $num; $u++) {
			$utf8Entities[$u] = '&#'.ord($entitiesDecoded[$u]).';';
		}
  		return str_replace ($htmlEntities, $utf8Entities, $input);
	}
But it leaves it intact. No errors, but no change either....
Any clue as to what might be going on?

Posted: Fri Aug 26, 2005 11:38 pm
by Stewsburntmonkey
Is there a reason you are declaring $rules and $changeto to be arrays? :)

Posted: Fri Aug 26, 2005 11:39 pm
by feyd
php will call html2unicode() before finalizing the string, thus neutralizing it's execution.

here's an example using preg_replace_callback()

Code: Select all

function doReplace($match)
{
  return "<br>Code:<br>\n<div style=\"border: 1px solid black; background: #9cdff1\">" . $this->html2unicode($match[1]) . "</div>";
}

function parseCode($input)
{
  return preg_replace_callback("#\<code\>(.*?)\</code\>#si",array(&$this,'doReplace'));
}

function html2unicode($input)
{
  // your function code here, blah blah
}

Posted: Sat Aug 27, 2005 8:45 am
by apg88
I put this into the code, but now it outputs anything.
Would it make a difference that it is in a class?

Code: Select all

function doReplace($match)
	{
  		return "<br>Code:<br>\n<div style=\"border: 1px solid black; background: #9cdff1\">" . $this->html2unicode($match[1]) . "</div>";
	}

	function parseCode($input)
	{
		return preg_replace_callback("#\<code\>(.*?)\</code\>#si",array(&$this,'doReplace'));
	}
I dont understand the

Code: Select all

array(&$this,'doReplace')
which i think could be the problem.

Posted: Sat Aug 27, 2005 8:49 am
by feyd

Posted: Mon Aug 29, 2005 9:48 am
by apg88
I finally got it working.

I started from scratch and found out I was missing an argument in the preg_replace_callbacl()

Code: Select all

function changeit($tochange)
	{
		$new_string = nl2br($this->html2unicode($tochange[1]));
		$finished_product = "<br>Code:<br>\n<div style=\"border: 1px solid black; background: #9cdff1\">" . $new_string . "</div>";
		return "$finished_product";
	}	
	
	function parseCode($input)
	{
		$finished = preg_replace_callback("#\<code\>\n*\r*(.*?)\</code\>#si",array(&$this,"changeit"),$input);
		return $finished;
	}