Jump to content


Photo

preg_match; preg_replace


  • Please log in to reply
5 replies to this topic

#1 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 02 October 2006 - 10:02 PM

I was trying to make my own template class but I ran into problems I don't quite  understand.

I use preg_match to find where loops start and end and read everything between. For some reason, it won't get the HTML when it matches. I can't figure out how to get it to show the HTML too.

On preg_replace, I can't get it to replace the loop strings themselves. such as {loop} info {/loop} it changes the 'info' part but the {loop} parts are still there. I don't understand why. I have to use str_replace to get rid of those but that seems ignorant since preg_replace SHOULD be doing it.

Any idea on why it's not getting the HTML and why its not replacing the {loop} areas?

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 02 October 2006 - 10:10 PM

Can you provide a small example? A couple of ideas:

  • You may be printing out the match result in the browser; doing so will cause the HTML to be rendered. Use the htmlentities function to see what you really matched.
  • Curly braces are metacharacters in regex--are you escaping them?

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 02 October 2006 - 10:27 PM

My codes on a different computer.. but let me try to remember how I set it..
(code probably isn't 100% accurate here)

preg_match("\/{$name/}(.*?)/{\/$name/}/s", $this->t_html, $matches);

and preg_replace is about the same. I first store $matches in another area $this->loop . I echoed that with htmlentities and nothing. I checked out the source of the page to make sure and it showed no html. So I'm not really sure. My brackets are escaped properly, I know that. Because I'm getting the correct area changes and code (for the most part).

when I did preg_match, I want everything saved.. such as: {loop} <b>{text}</b> {/loop}
all that with the html in it. then I'd like to replace all that after I'm done. I can get {text} and thats it.. thats all I can change too, which confuses me.

#4 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 02 October 2006 - 10:38 PM

  • Curly braces should be escaped with a backslash, not a forward.
  • Anything that you want saved you have surround in capturing parenthesis: ( ).
  • You should preg_quote your $name variable in case it contains any metacharacters.
 
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#5 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 03 October 2006 - 09:02 PM

I messed with it a little last night and got most of it working. I still have to use str_replace to get rid of the start and end of the loop tags though. I just added a function in there to kill all tags at the end of the script so I didn't keep doing it over and over and wasting time.

I'd still like to figure out whats going on but I know it's hard to help without the code. But it's something like this (just read it):

preg_match("/\{$name\}(.*?)\{\/$name\}/is", $this->t_html, $matches);

For the most part, it works. I THINK what might be the problem is my loops I save in the array. It saves without the loop tags and then I replace the templates html with what is in the loop array. Maybe it's just reading between the 2 tags and not the 2 tags AND everything between?

#6 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 03 October 2006 - 09:25 PM

You can't replace with a match function; try a callback:

<pre>
<?php

	function process ($matches) {
		return preg_replace('/\[(.+?)\]/', '<\1>', $matches[1]);
	}

	$test = 'before {loop}[b]middle[/b]{/loop} after';
	$name = 'loop';
	echo preg_replace_callback("/\{$name\}(.*?)\{\/$name\}/is", 'process', $test);

?>
</pre>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users