CrimpJiggler Posted April 2, 2014 Share Posted April 2, 2014 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 } Quote Link to comment https://forums.phpfreaks.com/topic/287480-preg_replace-how-to-use-variables-in-your-replace-string/ Share on other sites More sharing options...
requinix Posted April 2, 2014 Share Posted April 2, 2014 What is the value of $diagramPath? /e or not won't make a difference because you're doing the replacing just after defining the replacements - the value in $replace would be the same value during the /e execution. Quote Link to comment https://forums.phpfreaks.com/topic/287480-preg_replace-how-to-use-variables-in-your-replace-string/#findComment-1474754 Share on other sites More sharing options...
CrimpJiggler Posted April 3, 2014 Author Share Posted April 3, 2014 $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? Quote Link to comment https://forums.phpfreaks.com/topic/287480-preg_replace-how-to-use-variables-in-your-replace-string/#findComment-1474898 Share on other sites More sharing options...
requinix Posted April 3, 2014 Share Posted April 3, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287480-preg_replace-how-to-use-variables-in-your-replace-string/#findComment-1474901 Share on other sites More sharing options...
Ch0cu3r Posted April 4, 2014 Share Posted April 4, 2014 $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"]; Quote Link to comment https://forums.phpfreaks.com/topic/287480-preg_replace-how-to-use-variables-in-your-replace-string/#findComment-1474926 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.