Page 1 of 1

Pick out single letters

Posted: Thu May 28, 2009 4:04 am
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? :)

Re: Pick out single letters

Posted: Thu May 28, 2009 4:23 am
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?

Re: Pick out single letters

Posted: Thu May 28, 2009 6:05 am
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>

Re: Pick out single letters

Posted: Thu May 28, 2009 6:16 am
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.

Re: Pick out single letters

Posted: Thu May 28, 2009 7:12 am
by JKM
What should I use?

Thanks for the help!

Re: Pick out single letters

Posted: Thu May 28, 2009 1:38 pm
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');

Re: Pick out single letters

Posted: Thu May 28, 2009 4:18 pm
by JKM
Thanks a lot. What if I want a custom text for some of the letters?

Re: Pick out single letters

Posted: Fri May 29, 2009 1:21 am
by prometheuzz
JKM wrote:Thanks a lot. What if I want a custom text for some of the letters?
Then you change the code.