Jump to content

Recommended Posts

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); 
}
?>

Link to comment
https://forums.phpfreaks.com/topic/142720-matching-case-sensitive/
Share on other sites

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.

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.

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); 
}
?>

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;

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';

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.