Help - Search - Members - Calendar
Full Version: preg_match help
Invision Power Services > Community Forums > Community Web Design and Coding
Jeremy-
I need to find the text {[lang_whatever]} in my string, and then use the "whatever" (it can be anything) text later in another function. How can I do that?

In case that doesn't make sense, here's an example:

I have the HTML markup for an image stored in the database, and it looks like this:
<img alt="{[$lang_delete]}" src="delete.jpg" title="{[$lang_delete_title]}" />

(I insist on using {[$string]} becuase I want to match the way the language variables are called in my templates...)

I need to search the string (in this case the <img /> code) for each instance of {[$lang_whatever]} and then assign the "whatever" part as a variable that can be used later in an str_replace() as I have an array that will match the "whatever" with the keys in an array, and replace it with that particular key's value.
Michael_C
Why can't you just preg_replace in the first place?
Jeremy-
Perhaps I could, I just don't know how to go about doing that for what I need it to do. Anyone willing to provide an example?
Michael_C
First you need the array of language strings loaded(I suggest caching it to a PHP-file if it is stored in the database). Then you just need to run a preg_replace like:
CODE
$skin['bit_name'] = preg_replace("/{\[[$]lang_([a-z0-9_]*)\]}/ie", "\$lang['$1']", $skin['bit_name']);
_
CODE
$string = '\{[$lang_meh]\}';
$whatever = preg_replace("~\{\[\$lang_([a-zA-Z0-9]+?)\]\}~is", "\\1", $string);


That should make $whatever equal what you wanted.

The above posted would also work, but you have to escape the {s I think.
Jeremy-
This is what I am using:

CODE
   foreach ($macro_array as $macro) {

       $replacement = str_ireplace("<#ADMIN_IMG_DIR#>", $sokay->settings['admin_img_dir'], $macro['replacement']);
       $replacement = preg_replace("~\{\[\$lang_([a-zA-Z0-9]+?)\]\}~is", "\\1", $replacement);

       $html = str_ireplace( "<{".$macro['variable']."}>", $replacement, $html );

   }


<#ADMIN_IMG_DIR#> gets replaced with the image directory that the admin images reside, so for example, after that we have:

<img alt="{[$lang_edit]}" src="http://localhost/sokay/admin/images/edit.png" title="" />

So then, we do the preg_replace and it seems to not work blink.gif the alt text remains as {[$lang_edit]}. Also, is there anyway to get the replacement code (in this case, the "\\1" area to be a variable? Something like $sokay->lang['\\1'] ? That way I can automatically add the language replacements in there like normal.
Jeremy-
I think I have it:

CODE
   foreach ($macro_array as $macro) {

       $replacement = str_ireplace("<#ADMIN_IMG_DIR#>", $sokay->settings['admin_img_dir'], $macro['replacement']);
       $replacement2 = preg_match_all('|\{\[\$lang_(.*?)\]\}|i', $replacement, $mymatches);
     foreach ($mymatches[1] as $match){
         $replacement3 = str_ireplace('{[$lang_'.$match.']}', $sokay->lang[$match], $replacement);
     }


       $html = str_ireplace( "<{".$macro['variable']."}>", $replacement3, $html );


   }


It works, but let me know if there's a better way to do it. original.gif Thanks to all those that have helped.
Michael_C
Why have the overhead of a regex then a str_ireplace(which is PHP5 only BTW) when you could just use a regex which would be more efficient and work with PHP4.
Jeremy-
QUOTE(Michael_C @ Apr 5 2005, 03:51 AM)
Why have the overhead of a regex then a str_ireplace(which is PHP5 only BTW) when you could just use a regex which would be more efficient and work with PHP4.
*

You're right... and so was Veracon, the {'s do have to be escaped. I tried your code once before and it didn't work (that's why I tried another method) then I tried escaping the brackets and it works perfectly, thank you so much.

Here's the final code:

CODE
   foreach ($macro_array as $macro) {

       $replacement = str_replace("<#ADMIN_IMG_DIR#>", $sokay->settings['admin_img_dir'], $macro['replacement']);
       $replacement = preg_replace("/\{\[[$]lang_([a-z0-9_]*)\]\}/ie", "\$sokay->lang['$1']", $replacement);

       $html = str_replace( "<{".$macro['variable']."}>", $replacement3, $html );

   }



And thank you for brinnging up the fact that str_ireplace is PHP 5 only, I must have missed that when I was looking at the docs on it. I'll just go back to str_replace for now.
Michael_C
Hmm, I tested my code before posting and it worked without the braces escaped, I'm suing PHP 4.3.11.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.