1

Simple question for you folks. Sorry that I have to ask it.

On my website, I want to use signatures at "random" places in my text. The problem is, There could be multiple DIFFERENT signatures in this given string.

The signature code is ~~USERNAME~~

So anything like

~~timtj~~
~~foobar~~
~~totallylongusername~~
~~I-d0n't-us3-pr0p3r-ch@r@ct3r5~~

I have tried using preg_match for this, with no success. I understand that the third parameter is used to store the matches, but I can not properly get a match because of the format.

Should I not use preg_match, or am I just not able to use signatures in this manner?

timtj
  • 94
  • 2
  • 11
  • With the help of @Shankar and @max-m, using `preg_match_all` ended up being the best resort. Using this, it returns an array with 2 subarrays, `[0]` and `[1]`. With these arrays, you can use something similar to my code `return(str_replace($match[0],array_map("getsignature",$match[1]),$mystring));` where `getsignature` is a returnable function that searches a database or string for the username given by variable `$1` (ex: `function getsignature($username){`) – timtj Feb 23 '14 at 02:24

2 Answers2

6

You could make use of preg_match_all and with this modified regex

preg_match_all('/~~(.*?)~~/', $str, $matches);

enter image description here

The code...

<?php
$str="~~I-d0n't-us3-pr0p3r-ch@r@ct3r5~~";
preg_match_all('/~~(.*?)~~/', $str, $matches);
print_r($matches[1]);

OUTPUT :

Array
(
    [0] => I-d0n't-us3-pr0p3r-ch@r@ct3r5
)
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • I like the looks of it, however, when I run this with `$str="Here is my signature: ~~timtj~~"`, it returns an array, with sublevel arrays containing `~~timtj~~` AND `timtj`. I would like it to only return the one without the tildes. – timtj Feb 23 '14 at 02:02
  • @timtj, I have showed you the results `without the tildes` as expected. As you can see there is no `tildes` in the output. – Shankar Narayana Damodaran Feb 23 '14 at 02:07
  • 2
    I see now. Upon closer inspection, I have omitted the `[1]` from your example, and with it, it works beautifully. Accepted answer and upvote. Thanks for expressing your knowledge in a simple way! – timtj Feb 23 '14 at 02:08
4

This should work, but usernames mustn't contain ~~

preg_match_all('!~~(.*?)~~!', $str, $matches);

Output:

Array
(
    [0] => Array
        (
            [0] => ~~timtj~~
            [1] => ~~foobar~~
            [2] => ~~totallylongusername~~
            [3] => ~~I-d0n't-us3-pr0p3r-ch@r@ct3r5~~
        )


    [1] => Array
        (
            [0] => timtj
            [1] => foobar
            [2] => totallylongusername
            [3] => I-d0n't-us3-pr0p3r-ch@r@ct3r5
        )
)

The first sub array contains the complete matched strings and the other sub arrays contain the matched groups.


You could change the order by using the flag PREG_SET_ORDER, see http://php.net/preg_match_all#refsect1-function.preg-match-all-parameters

<?php
$str = "~~timtj~~ ~~foobar~~ ~~totallylongusername~~ ~~I-d0n't-us3-pr0p3r-ch@r@ct3r5~~";
preg_match_all("!~~(.*?)~~!", str, $matches, PREG_SET_ORDER);
print_r($matches);

This code produces the following output

Array
(
    [0] => Array
        (
            [0] => ~~timtj~~
            [1] => timtj
        )

    [1] => Array
        (
            [0] => ~~foobar~~
            [1] => foobar
        )

    [2] => Array
        (
            [0] => ~~totallylongusername~~
            [1] => totallylongusername
        )

    [3] => Array
        (
            [0] => ~~I-d0n't-us3-pr0p3r-ch@r@ct3r5~~
            [1] => I-d0n't-us3-pr0p3r-ch@r@ct3r5
        )
)
max-m
  • 847
  • 7
  • 9
  • This code works well. Not sure what the difference is in relation to having `!` at the beginning and end of the exp as opposed to `/`, but I am willing to accept that this code works – timtj Feb 23 '14 at 02:09
  • 1
    @timtj, They are just the delimiters for an regular expression you can even add `@` !! :) – Shankar Narayana Damodaran Feb 23 '14 at 02:11
  • 1
    @timtj you can use any non-alphanumeric, non-backslash, non-whitespace character as delimiter, see http://php.net/manual/en/regexp.reference.delimiters.php – max-m Feb 23 '14 at 02:11
  • 2
    Just when you think you know enough to get the job done, PHP always has something for someone to learn. – timtj Feb 23 '14 at 02:14