Closed Thread Icon

Topic awaiting preservation: PHP String-Replacement Problem Pages that link to <a href="https://ozoneasylum.com/backlink?for=25915" title="Pages that link to Topic awaiting preservation: PHP String-Replacement Problem" rel="nofollow" >Topic awaiting preservation: PHP String-Replacement Problem\

 
Author Thread
Wes
Paranoid (IV) Mad Scientist

From: Inside THE BOX
Insane since: May 2000

posted posted 05-31-2005 07:36

Quick one: I'm parsing a string using an array of search/replace items, but I'm tired and can't figure out how to do this one properly.

Basically, it's like this:

code:
$replacements = array(
	"Company Name, Inc."	=>	"<span class=\"style\">Company Name, Inc.</span>",
	"Company Name"		=>	"<span class=\"style\">Company Name</span>",
	"exampletag"		=>	"Example",
	);

foreach ($replacements as $search => $replace) {
	$data = ereg_replace("[{]?" . $search . "[}]?", $replace, $data);
}


The reason for the curly brackets in the regex is because some replacement items are placeholder tags for other data that appear as {exampletag}, while others are just common phrases I need styled wherever they occur in the copy, like the company name.

My problem is with items like the company name. Sometimes it appears as "Company Name, Inc." and sometimes just as "Company Name." The problem with the technique I'm using is that I end up with <span class=\"style\"><span class=\"style\">Company Name</span>, Inc.</span> when it's done.

I'm far too tired to think anymore ... can anyone take over my brain functions, please?



I leave all these to show how tired I am:

(Edited by Wes on 05-31-2005 07:41)

(Edited by Wes on 05-31-2005 07:42)

(Edited by Wes on 05-31-2005 07:43)

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 05-31-2005 09:09

well... the way to do this is
a) Transform longer expressions first.
b) don't replace if there's a > before your expression.

I could do this easily with perl style regular expressions - but I never bothered to learn eregs.

code:
function compareByLength($strA,$strB) //this might be the wrong way around. replace -1 and 1 with each other.
{
   if (strlen($strA) > strlen($strB))
      return 1;
   elseif (strlen($strA) < strlen($strB))   
     return -1;
   else
     return 0;
}

$replacements = array(
"Company Name, Inc."=>"<span class=\"style\">Company Name, Inc.</span>",
"Company Name"=>"<span class=\"style\">Company Name</span>",
"exampletag"=>"Example",
);
usort($replacements,'compareByLength');

foreach ($replacements as $search => $replace) {
$data = preg_replace("/[^>]{1}{?" . $search . "}?", $replace, $data);
}



would be a rough outline of what you'd have to do.

jiblet
Paranoid (IV) Inmate

From: Minneapolis
Insane since: May 2000

posted posted 05-31-2005 23:42

Or you could just change the first pair to:

"Company Name(, Inc.)?" => "<span class=\"style\">Company Name\\1</span>"

and get rid of the second pair.

-jiblet

(Edited by jiblet on 05-31-2005 23:42)

Wes
Paranoid (IV) Mad Scientist

From: Inside THE BOX
Insane since: May 2000

posted posted 06-03-2005 17:56

Hey, that works beautifully. I hadn't thought of putting regular expressions in the array itself. And I learned about backreferences.

Thanks, guys.

AT
Bipolar (III) Inmate

From: Gainesboro, TN, USA
Insane since: Aug 2000

posted posted 06-08-2005 20:24

Ah, I didn't ask the question but it was a good response on both TB and jiblet.

I'd rep you

Casey / AT
Personal
Song Lyrics
Family Recipes

« BackwardsOnwards »

Show Forum Drop Down Menu