Pick out single letters

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Pick out single letters

Post by JKM »

Code: Select all

<?php 
$search = array(
    '/(.*?)a(.*?)/',
    '/(.*?)b(.*?)/',);
    '/(.*?)c(.*?)/',);
    '/(.*?)d(.*?)/',);
    '/(.*?)e(.*?)/',);
    '/(.*?)f(.*?)/',);
    '/(.*?)g(.*?)/',);
$replace = array(
    '<span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter A">a</span>',
    '<span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter B">b</span>',
    '<span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter C">c</span>',
    '<span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter D">d</span>',
    '<span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter E">e</span>',
    '<span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter F">f</span>',
    '<span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter G">g</span>',);
    echo preg_replace($search, $replace, $fetch['criteria_'.$what.'_list']); ?>
The field 'criteria_XXX_list' could be like this:
abcdefg
bcdefg
acdg
cge
ag
a
Since 'abcdefg' returns 'ggggggggggggggggggggggggggggggggggggggggggg', there is something wrong with my regex.

Anyone? :)
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Pick out single letters

Post by prometheuzz »

JKM wrote:...Since 'abcdefg' returns 'ggggggggggggggggggggggggggggggggggggggggggg', there is something wrong with my regex.

Anyone? :)
Two remarks:
1 - your code snippet will not run properly, it contains syntax errors;
2 - the code snippet (when fixed) will not produce 'ggggggggggggggggggggggggggggggggggggggggggg' when executing it like this:

Code: Select all

echo preg_replace($search, $replace, 'abcdefg');
Feel free to post a better phrased question if you like. Instead, try to describe what it is you're trying to do. What should the string 'abcdefg' be replaced with, for example?
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: Pick out single letters

Post by JKM »

Thanks for the answer.

1) What syntax error(s)?

If the field criteria_XXX_list is 'abc', it should return:

Code: Select all

<span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter A">a</span><span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter B">b</span><span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter C">c</span>
If the field criteria_XXX_list is 'a', it should return:

Code: Select all

<span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter A">a</span>
If the field criteria_XXX_list is 'abcdefg', it should return:

Code: Select all

<span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter A">a</span><span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter B">b</span><span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter C">c</span><span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter D">d</span><span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter E">e</span><span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter F">f</span><span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter G">g</span>
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Pick out single letters

Post by prometheuzz »

JKM wrote:Thanks for the answer.

1) What syntax error(s)?
...
Well, that can be easily found out by running the code from your original post through a PHP interpreter.
Try to execute this:

Code: Select all

$search = array(
    '/(.*?)a(.*?)/',
    '/(.*?)b(.*?)/',);
    '/(.*?)c(.*?)/',);
    '/(.*?)d(.*?)/',);
    '/(.*?)e(.*?)/',);
    '/(.*?)f(.*?)/',);
    '/(.*?)g(.*?)/',)
Or just look at it closely: it has many error in it.
Also, I don't see how your code produces 'gggggggggggggggggggggggggggggg'.

Anyway, let's say you have a string 'ab' which you want to replace with:

Code: Select all

<span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter A">a</span>
<span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter B">b</span>
(I added a line break for clarity)

Doing it like you are planning it to do, will first replace the 'a' with

Code: Select all

<span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter A">a</span>
resulting in a "temporary" string:

Code: Select all

<span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter A">a</span>
b
and now all 'b'-s will be replaced by:

Code: Select all

<span style="cursor: help; border-bottom: 1px dotted #000;" title="This is the letter B">b</span>
But that will cause the 'b' from 'border' and the 'b' from 'bottom' to also be replaced! In short: don't use preg_replace for this.
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: Pick out single letters

Post by JKM »

What should I use?

Thanks for the help!
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Pick out single letters

Post by prometheuzz »

JKM wrote:What should I use?

Thanks for the help!
I know jack about PHP, so my suggestion may very well be not so PHP-esque, but here's how you could do it:

Code: Select all

function custom_replace($text) {
    $return_val = "";
    for ($i = 0; $i < strlen($text); $i++) {
        $return_val .= "<span style=\"cursor: help; border-bottom: 1px dotted ".
                    "#000;\" title=\"This is the letter ".strtoupper($text{$i}).
                    "\">".$text{$i}."</span>";
    }
    return $return_val;
}
 
echo custom_replace('abc');
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: Pick out single letters

Post by JKM »

Thanks a lot. What if I want a custom text for some of the letters?
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Pick out single letters

Post by prometheuzz »

JKM wrote:Thanks a lot. What if I want a custom text for some of the letters?
Then you change the code.
Post Reply