Jump to content

preg_replace - How to use variables in your replace string


CrimpJiggler

Recommended Posts

Heres the code:

		/* Diagrams */
		$match["img"] = "~\[img=(.*?) type=(.*?)]~i";
		$replace["img"] = "<img src=\"/path/to/img/$1-$2.gif\">";
		
		/* Diagrams */
		$match["diagram"] = "~\[diagram=(.*?)]~i";
		$replace["diagram"] = "<img src=\"{$diagramPath}/$1\">";

		$content = preg_replace($match,$replace,$content);

its a simple BBCode type script so its useful the way I can put all the patterns into the $match array, and automatically replace it with values from the $replace array. works fune because the path is hardcoded, but diagram won't work, it just ignores the $diagramPath variable. I've tried different things like changing to single quotes:

		$replace["diagram"] = '<img src="' . $diagramPath . '/$1">';

but I get the same results, it reads the $1 variable, but won't read any custom variables that I put in there. I want to keep this simple $match $replace array system, is there a way to make it read all variables in the $replace string? I see that the /e modifier is deprecated, and from what I read it sounds like thats what I'm looking for, so I followed the manuals advice and tried using preg_replace_callback but it got messy, it wouldn't automatically use the $replace array.

 

The only use I have for preg_replace() is situations like this, otherwise I'll use str_replace or preg_match. I don't see the purpose for preg_replace_callback when you can just use if(preg_match()) { // do something }

 

Link to comment
Share on other sites

Not in the replacement string. If there were $s or \s though, they could cause a problem.

 

It looks fine to me. What are the exact values of $match, $replace, and $content before and after? If you're outputting them to HTML, be sure to look at the actual source of the page and not what your browser's DOM inspection thing may show.

Link to comment
Share on other sites

$diagramPath is the full path to the folder so its something like /var/www/mysite/app/webroot/img/diagrams. Could the / slashes be causing problems?

That path is not suitable to be used on a webpage. The path to the image should be relative from your url. ie, mysite.com maps to the document root folder (/var/www/mysite/app/webroot/ is your path document root folder), So to link to an image stored in /var/www/mysite/app/webroot/img/diagrams then $diagramPath should be set to /img/diagrams/. Note the / at the start of a url paths means the root of url (mysite.com).

 

Also can you tell us where this code is being ran from? Is it part of a function? Variables defined in double quoted strings should be expanded and not ignored, except the $1, $2 vars, these will be expanded by preg_replace with the matches returned from your regex patterns.

 

Does the following show the correct value for your replacement?

echo $replace["diagram"];
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.