Jump to content

Regex question


dbo

Recommended Posts

This is really more of a regular expression question, but I thought it might be basic enough that I didn't need to post there... we'll see.

 

Assume I have the following string:

 


$str = "<?php echo $PH_title; ?> some text blah blah <?php echo $PH_right; ?>";

 

The end goal is to write a regular expression that would give me title and right. I think that the $ might be causing fits or something?

Link to comment
Share on other sites

Here's the dumbed down proof of concept code I'm playing with right now. So far, I'm not proving much :)

 

<?php

$str = "<?php echo \$PH_title; ?>";

preg_match_all("/\<\?php echo \$PH_title; \?\>/", $str, $matches);

print_r($matches);

?>

Link to comment
Share on other sites

Yeah I'm familiar with preg_match_all. I used to be quiet a bit better at writing regular expressions. I don't seem to use them nearly as often as when I programmed more in Perl.

 

While I fight with this... the longer answer of what I'm doing is pulling out content from a database, doing some manipulation and then creating a hunk of code that needs run through eval (yes I know security concerns, blah blah blah) it's just tinkering at this point, but I have some neat ideas.

Link to comment
Share on other sites

I'm good again now. You're both right...

 

The first problem was that what I was trying to parse read as ||PH_title|| rather than.. <?php echo $PH_title; ?> because of some other playing I was doing... go figure, the pattern didn't match.

 

Thanks for all the quick help. You guys saved me some hassles. I'm kinda sad really. I've been doing a lot of C#.NET development lately, and apparently now that I'm playing with PHP again I'm the suck :(

Link to comment
Share on other sites

Again, just to elaborate on why I needed to do this...

 

Based on the URI of the current page, I query the database for page information. Next I grab the appropriate template file and parse out any "placeholder" (PH) variables that live in the template. I'm then going to query the variable table for the current page and then start to assemble a page. Right now there is no GUI, but the idea is to make sort of a quasi CMS sorta thing.

Link to comment
Share on other sites

I'm not always going to be dropping in static HTML and I'm trying not to introduce any special syntax/language for identifying the placeholders. I guess I could potentially evaluate the code once pulled from the DB and then string replace in the rendered results, but I don't know that it would buy me much.

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.