Jump to content

preg_match; preg_replace


Jocka

Recommended Posts

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?
Link to comment
Share on other sites

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

[list]
[*]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?
[/list]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[list]
[*]Curly braces should be escaped with a backslash, not a forward.
[*]Anything that you want saved you have surround in capturing parenthesis:[tt] ( )[/tt].
[*]You should[tt] preg_quote [/tt]your[tt] $name [/tt]variable in case it contains any metacharacters.
[/list] 
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

You can't [i]replace[/i] with a [i]match[/i] function; try a callback:

[code]
<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>
[/code]
Link to comment
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.