Welcome Guest, Not a member yet? Register   Sign In
Pattern Matching - small problem is a head scratcher for me
#1

[eluser]tinawina[/eluser]
I have a site that archives research reports. We store the titles so that any title starting with "The", "An", or "A" is reformatted with those words at the end. Example:

"An Annotated Study" => becomes => "Annotated Study, An"

Here's the code used to make this change:

Code:
$title = "An Annotated Study";

if ( eregi("^[An ]{3}", $title) ) {
    $title2 = eregi_replace ("^[An ]{3}", "", $title);
    $title = eregi_replace ($title2, $dtitle2 . ", An", $title2);
}

echo $title; // echoes "Annotated Study, An" which is correct.

When this is run the first time, all is well and we end up with the correct title. However, if this is run on this reformatted title a second time we get:

"notated Study, An, An"

Why isn't the initial eregi picking up on the required space and required 3 characters? I've tried this with preg_match as well, same thing happens.

Any help is much appreciated. Thanks!
#2

[eluser]Bryan Seeds[/eluser]
hmm, can't say I know why, but maybe try this
Code:
eregi("^[An\s]{3}", $title)

or another variation with the addition of the '\s'.

-B
#3

[eluser]Bryan Seeds[/eluser]
Ok, so I actually ran the code, and seeing how bracket expressions specify which characters are allowed in a single position of a string, I tried the first if conditional as

Code:
if(eregi("^An\s{3}", $title)){

}

and that returned false. maybe of some help.

-B
#4

[eluser]Bryan Seeds[/eluser]
So after I played again, I found my last p0st didn't do the trick.. but here you go :

Code:
$title = "An Annotated Study";

if (!eregi("^[An\s]{3}", $title) ) {
    $title2 = eregi_replace ("^[An\s]{3}", "", $title);
    $title = eregi_replace ($title2, $title2 . ", An", $title2);
}

echo $title; // echoes "Annotated Study, An" which is correct.

Run again on <code>$title</code> and the condition is <code>false</code> so it will <code>echo</code> out the same.

-B
#5

[eluser]sophistry[/eluser]
@tinawina... two things:

1) use preg_ functions - ereg_ functions are going to be deprecated in PHP6 so prepare now by switching. they are the same and preg_is faster (according to the PHP.net site)

2) square brackets are use to define a character class. you have capital A and lowercase n and space in a character class which means look for capital A, n or space (in any order) 3 times starting at the beginning of the string. first, you need to remove the square brackets.

try this OTTOMH: ^(The|An?)\s

that says look for start of string, followed by literal string The OR A OR A followed by n. Then look for one space. if you need one or more spaces add a plus sign after the \s.

cheers.

EDIT: for the reinsertion, you'll use a backreference in your replacement pattern. then you will only need one call to preg_replace(). try to figure it out and if you can't post here again and i'll see if i can help.
#6

[eluser]tinawina[/eluser]
Hi and thanks Bryan and sophistry for your replies. I'm only just now getting back to this today. Hectic... Last night I added a line to my code:


Code:
$title = "Annotated Study, An";

if ( eregi("^[An ]{3}", $title) ) {
    $title2 = eregi_replace ("^[An ]{3}", "", $title);
    $title = eregi_replace ($title2, $dtitle2 . ", An", $title2);
    $title = eregi_replace (", An, An", ", An", $title);
}
echo $title; // echoes "Annotated Study, An" which is correct.

I know this is a hack but it was midnight and I'd been coding for too long. I will try out both of your suggestions and post back asap. Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB