dbo Posted September 18, 2008 Share Posted September 18, 2008 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 https://forums.phpfreaks.com/topic/124755-regex-question/ Share on other sites More sharing options...
F1Fan Posted September 18, 2008 Share Posted September 18, 2008 Are you getting any errors? Link to comment https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644393 Share on other sites More sharing options...
DarkWater Posted September 18, 2008 Share Posted September 18, 2008 What do you mean? How would you have the php tags in the string and expect output? And why is it in in " " if you don't want variables parsed out by PHP? Explain it a bit more. =P Link to comment https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644394 Share on other sites More sharing options...
genericnumber1 Posted September 18, 2008 Share Posted September 18, 2008 use single quotes around the $str = definition, it will keep php from replacing the variables with their values... or you can just escape the $... \$ edit: as dark beat me to... barely Link to comment https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644395 Share on other sites More sharing options...
dbo Posted September 18, 2008 Author Share Posted September 18, 2008 I want to parse a literal string that contains those tags. And yes I escaped the $. No I'm not receiving errors, I'm just not getting the variables out that I want. Link to comment https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644401 Share on other sites More sharing options...
dbo Posted September 18, 2008 Author Share Posted September 18, 2008 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 https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644405 Share on other sites More sharing options...
genericnumber1 Posted September 18, 2008 Share Posted September 18, 2008 I'm fairly sure you don't need to escape < >.. replace title with (.*) and play with the parser's greed and you should be able to pull it out without much effort. EDIT: Give this a try: <\?php echo \$PH_(.*?); \?> Link to comment https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644409 Share on other sites More sharing options...
DarkWater Posted September 18, 2008 Share Posted September 18, 2008 <?php $str = '<?php echo "${PH_title}foo;" ?> some text blah blah <?php echo $PH_right; ?>'; $matches = array(); preg_match('/(\${?(.+?)(}|\b))/i', $str, $matches); print_r($matches); Added the { } to mix it up a bit. Link to comment https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644412 Share on other sites More sharing options...
DarkWater Posted September 18, 2008 Share Posted September 18, 2008 It'll parse out ANY variable name in a PHP string, btw. Not just your $PH_ ones. Link to comment https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644415 Share on other sites More sharing options...
genericnumber1 Posted September 18, 2008 Share Posted September 18, 2008 It'll parse out ANY variable name in a PHP string, btw. Not just your $PH_ ones. Why do more when you only need 1 feature? Less complexity is better, I'll never convince you of this, darkwater! Link to comment https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644421 Share on other sites More sharing options...
dbo Posted September 18, 2008 Author Share Posted September 18, 2008 Erm. I tried genericnumber's solution, no love there. I'll fuss with DarkWater's a bit. It's not quite the same as what I'm after but I might be able to make it work. Link to comment https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644424 Share on other sites More sharing options...
DarkWater Posted September 18, 2008 Share Posted September 18, 2008 I honestly thought he needed more than one variable when I wrote the regex, and THEN I saw all of these posts and his "attempt" at it, which threw me off. >_< Link to comment https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644425 Share on other sites More sharing options...
DarkWater Posted September 18, 2008 Share Posted September 18, 2008 @dbo: If you use preg_match_all(), your variable names are contained in $matches[0]. You can run a foreach on it. Link to comment https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644426 Share on other sites More sharing options...
genericnumber1 Posted September 18, 2008 Share Posted September 18, 2008 Mine works fine, just tried it. $str = '<?php echo $PH_title; ?> some text blah blah <?php echo $PH_right; ?>'; preg_match_all('/<\?php echo \$PH_(.*?); \?>/', $str, $matches); print_r($matches); were you perhaps using double quotes around your pattern? Link to comment https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644432 Share on other sites More sharing options...
dbo Posted September 18, 2008 Author Share Posted September 18, 2008 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 https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644433 Share on other sites More sharing options...
genericnumber1 Posted September 18, 2008 Share Posted September 18, 2008 The best tool for getting back your regex mojo: The Regex Coach. Real-time regex matching with tons of features. Link to comment https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644435 Share on other sites More sharing options...
dbo Posted September 18, 2008 Author Share Posted September 18, 2008 @GN1 You're right, I had it in double quotes. Thanks for the catch. Link to comment https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644437 Share on other sites More sharing options...
genericnumber1 Posted September 18, 2008 Share Posted September 18, 2008 @GN1 You're right, I had it in double quotes. Thanks for the catch. Yeah, I've gotten in the practice of just using single quotes for everything unless I explicitly need variable replacement... of course that gets me in trouble when I move back to C++ and various other languages. Link to comment https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644438 Share on other sites More sharing options...
dbo Posted September 18, 2008 Author Share Posted September 18, 2008 Now the trick is figuring out how to treat data pulled from the db as literal Link to comment https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644443 Share on other sites More sharing options...
DarkWater Posted September 18, 2008 Share Posted September 18, 2008 It should already be literal. Link to comment https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644444 Share on other sites More sharing options...
genericnumber1 Posted September 18, 2008 Share Posted September 18, 2008 I think he means parse it for variables. treat it as a literal in double quotes perhaps? I'm sure there's a simple/non-messy way I just can't think of... Link to comment https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644445 Share on other sites More sharing options...
dbo Posted September 18, 2008 Author Share Posted September 18, 2008 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 https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644448 Share on other sites More sharing options...
dbo Posted September 18, 2008 Author Share Posted September 18, 2008 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 https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644450 Share on other sites More sharing options...
genericnumber1 Posted September 18, 2008 Share Posted September 18, 2008 could always just do a simple str_replace()? would be considerably better than extracting an array of the vars and using (don't spit on me here)... eval(). Link to comment https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644457 Share on other sites More sharing options...
dbo Posted September 18, 2008 Author Share Posted September 18, 2008 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 https://forums.phpfreaks.com/topic/124755-regex-question/#findComment-644463 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.