Scooby08 Posted January 28, 2009 Share Posted January 28, 2009 I'm trying to preg_replace the following, but it seems to only find the base href in caps.. What can I do to fix?? Or is it possible to search for both with one search line?? if that makes any sense?? <?php function TranslateVariables($input) { global $SITEURL; $search = array("/<BASE HREF=\"(.*?)\">/", "/<base href=\"(.*?)\" \/>/" ); $replace = array("<BASE HREF=\"$SITEURL/\">", "<base href=\"$SITEURL/\" />" ); return preg_replace ($search, $replace, $input); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142720-matching-case-sensitive/ Share on other sites More sharing options...
nrg_alpha Posted January 28, 2009 Share Posted January 28, 2009 Would this more along the lines of what you are looking for? $search = '#<BASE HREF=[\'"]([^\'"]+)[\'"]>#i' Quote Link to comment https://forums.phpfreaks.com/topic/142720-matching-case-sensitive/#findComment-748123 Share on other sites More sharing options...
.josh Posted January 28, 2009 Share Posted January 28, 2009 in case you didn't notice, what nrg_alpha did was add the i modifier after the delimiter. #blahblah#i which makes it case-insensitive. Quote Link to comment https://forums.phpfreaks.com/topic/142720-matching-case-sensitive/#findComment-748127 Share on other sites More sharing options...
DarkWater Posted January 28, 2009 Share Posted January 28, 2009 Would this more along the lines of what you are looking for? $search = '#<BASE HREF=[\'"]([^\'"]+)[\'"]>#i' If you're matching quotes, you might want to try something like: '/<base href=([\'"])(.+?)\1>/i' That way the quotes are matched. Quote Link to comment https://forums.phpfreaks.com/topic/142720-matching-case-sensitive/#findComment-748128 Share on other sites More sharing options...
Scooby08 Posted January 28, 2009 Author Share Posted January 28, 2009 I have tried these suggestions and this is the error I get and I'm not quite sure what it is meaning.. Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Quote Link to comment https://forums.phpfreaks.com/topic/142720-matching-case-sensitive/#findComment-748137 Share on other sites More sharing options...
.josh Posted January 28, 2009 Share Posted January 28, 2009 post your new code Quote Link to comment https://forums.phpfreaks.com/topic/142720-matching-case-sensitive/#findComment-748139 Share on other sites More sharing options...
nrg_alpha Posted January 28, 2009 Share Posted January 28, 2009 Oops.. yeah, forgot to explain the i modifier...my bad. Thanks CV. @Darkwater... yes, you can do that to match quotes... I just have a tendency to look for either single or double quotes (which admittedly does look wierd matching something like "blah'. Odds are, web code wouldn't mix and match.. but for neatness, sure.. you could do that I would recommend using negated character classes such as ([^\'"]+) as opposed to (.+?), as they are [generally] faster. Quote Link to comment https://forums.phpfreaks.com/topic/142720-matching-case-sensitive/#findComment-748141 Share on other sites More sharing options...
Scooby08 Posted January 28, 2009 Author Share Posted January 28, 2009 Here is my new code.. <?php function TranslateVariables($input) { global $SITEURL; $search = array('/<base href=([\'"])(.+?)\1>/i' ); $replace = array("<base href=\"$SITEURL/\" />" ); return preg_replace ($search, $replace, $input); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142720-matching-case-sensitive/#findComment-748145 Share on other sites More sharing options...
.josh Posted January 28, 2009 Share Posted January 28, 2009 there's nothing wrong with that code. Your error is somewhere else. Quote Link to comment https://forums.phpfreaks.com/topic/142720-matching-case-sensitive/#findComment-748153 Share on other sites More sharing options...
Scooby08 Posted January 28, 2009 Author Share Posted January 28, 2009 I switched it to this and it got rid of the error, but it is not finding any matches.. <?php function TranslateVariables($input) { global $SITEURL; $search = array("/<base href=(['\"])(.+?)\1>/i" ); $replace = array("<base href=\"$SITEURL/\" />" ); return preg_replace ($search, $replace, $input); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142720-matching-case-sensitive/#findComment-748154 Share on other sites More sharing options...
nrg_alpha Posted January 28, 2009 Share Posted January 28, 2009 You shouldn't need to use arrays for $search and $replace, as they both contain one value each.. Is this along the lines of what you are looking for? $str = '<BASE HREF="hello">'; $SITEURL = "goobye"; function TranslateVariables($input, $TARGET) { $search = '/<base href=([\'"])[^\'"]+\1>/i'; $replace = "<base href=\"$TARGET\" />"; return preg_replace ($search, $replace, $input); } echo TranslateVariables($str, $SITEURL); Edit... instead of echoing, you can use the initial string instead.. so if there is a match, the string itself will be changed.. if not, nothing happens... $str = TranslateVariables($str, $SITEURL); echo $str; Quote Link to comment https://forums.phpfreaks.com/topic/142720-matching-case-sensitive/#findComment-748162 Share on other sites More sharing options...
.josh Posted January 28, 2009 Share Posted January 28, 2009 Are you sure you're looking in the right place for those returns? Using arrays as your parameters will return a multi-dim array. Quote Link to comment https://forums.phpfreaks.com/topic/142720-matching-case-sensitive/#findComment-748165 Share on other sites More sharing options...
Scooby08 Posted January 28, 2009 Author Share Posted January 28, 2009 I do have multiple values in the arrays, I just took out the rest to show my problem.. Here is the full function: <?php function TranslateVariables($input, $props = "") { global $VAR, $TEMPL, $sitedir, $USER, $SITEHOMEURL, $SITEURL; $search = array("/<title>.*?<\/title>/im", "/<meta name=\"keywords\" content=\".*?\">/im", "/<meta name=\"keywords\" content=\".*?\" \/>/im", "/<meta name=\"description\" content=\".*?\">/im", "/<meta name=\"description\" content=\".*?\" \/>/im", "/<\!--CompanyName-->.*?<!--\/CompanyName-->/", "/<\!--TagLine-->.*?<!--\/TagLine-->/", "/<\!--Footer-->.*?<!--\/Footer-->/", "/<BASE HREF=\"(.*?)\">/", "/##USERNAME##/", "/##URL##/", "/##EMAIL##/", "/##PHONE##/", "/##COMPANY##/", "/##ADDR1##/", "/##ADDR2##/", "/##FAX##/", "/##OWNER##/", "/".urlencode("##COMPANY##")."/", "/".urlencode("##TAGLINE##")."/" ); $replace = array("<title>".EncodeText($VAR['title'])." - ".($props['title'])."</title>", "<meta name=\"keywords\" content=\"".(stripslashes($props['keywords']))."\">", "<meta name=\"keywords\" content=\"".(stripslashes($props['keywords']))."\" />", "<meta name=\"description\" content=\"".(stripslashes($props['description']))."\">", "<meta name=\"description\" content=\"".(stripslashes($props['description']))."\" />", "<!--CompanyName-->".EncodeText($VAR['title'])."<!--/CompanyName-->", "<!--TagLine-->".EncodeText($VAR['tagline'])."<!--/TagLine-->", "<!--Footer-->".EncodeText($VAR['footer'])."<!--/Footer-->", "<BASE HREF=\"$SITEURL/\">", "$USER", $VAR['url'], $VAR['email'], $VAR['phone'], $VAR['title'], $VAR['addr1'], $VAR['addr2'], $VAR['fax'], $VAR['owner'], urlencode($VAR['title']), urlencode($VAR['tagline']) ); if (strlen($VAR['logo'])>0) { $search[] = "/<!--CorpLogo-->/"; $replace[] = "<a href=\"index.php\"><img border=0 src=\"".$VAR['logo']."\" ".$TEMPL['logohtml']."></a>"; } return preg_replace ($search, $replace, $input); } ?> I have a question.. What does the +\1 mean in this line: '/<base href=([\'"])[^\'"]+\1>/i'; Quote Link to comment https://forums.phpfreaks.com/topic/142720-matching-case-sensitive/#findComment-748169 Share on other sites More sharing options...
.josh Posted January 28, 2009 Share Posted January 28, 2009 \1 is the data captured from the first (..) match. Quote Link to comment https://forums.phpfreaks.com/topic/142720-matching-case-sensitive/#findComment-748173 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.