Hi.
I need to search some submitted text for "code-*" and return all unique occurences of whole words.
Where the * part of the word can be any number and/or alphabetic letter and/or "-" and/or "_"
Examples of valid words could be:
Note: there can be multiple instances of the same word
code-black-on-white
code-gray_on_black
code-black-on-white
code-156-green_cows
The word(s) I'm searching for will allways be in the form:
"code-*" and will allways start and end in double quotes.
I would like to end up with a comma-separated list of unique words without the double quotes.
I have tried to use preg_match_all but without luck.
Got this tips from another forum: and it actually works as long as text is NOT put in same line in PHP like this:
$text='<div class="code-cool">Bla bla blakkkskk</div>
This is just some bla text. Bla bla bla.
<div class="code-super">More bla here</div>More text to search here.
<div class="code-cool">More bla here</div>More text to search here.';
$pattern='/("code-.+")/';
preg_match_all($pattern,$text,$matches);
$matches=$matches[1];
$matches_unique=array_unique($matches);//remove duplicates
echo $matches_string=implode(',',$matches_unique);
Here the echo outputs:
"code-cool","code-super"
which is what I want.
Then after a while I started to test with live data and found that the tips I got only worked on the example text I submitted with linebreaks... and not my live data.
If the text in the first example is put on one line like this in PHP:
$text='<div class="code-cool">Bla bla blakkkskk</div>This is just some bla text. Bla bla bla.<div class="code-super">More bla here</div>More text to search here.<div class="code-cool">More bla here</div>More text to search here.';
then the output is this:
"code-cool">Bla bla blakkkskkThis is just some bla text. Bla bla bla.
More bla here
More text to search here.
Which is definitely not what I want.
Okay... Anybody that takes the challenge?
I need to search some submitted text for "code-*" and return all unique occurences of whole words.
Where the * part of the word can be any number and/or alphabetic letter and/or "-" and/or "_"
Examples of valid words could be:
Note: there can be multiple instances of the same word
code-black-on-white
code-gray_on_black
code-black-on-white
code-156-green_cows
The word(s) I'm searching for will allways be in the form:
"code-*" and will allways start and end in double quotes.
I would like to end up with a comma-separated list of unique words without the double quotes.
I have tried to use preg_match_all but without luck.
Got this tips from another forum: and it actually works as long as text is NOT put in same line in PHP like this:
$text='<div class="code-cool">Bla bla blakkkskk</div>
This is just some bla text. Bla bla bla.
<div class="code-super">More bla here</div>More text to search here.
<div class="code-cool">More bla here</div>More text to search here.';
$pattern='/("code-.+")/';
preg_match_all($pattern,$text,$matches);
$matches=$matches[1];
$matches_unique=array_unique($matches);//remove duplicates
echo $matches_string=implode(',',$matches_unique);
Here the echo outputs:
"code-cool","code-super"
which is what I want.
Then after a while I started to test with live data and found that the tips I got only worked on the example text I submitted with linebreaks... and not my live data.
If the text in the first example is put on one line like this in PHP:
$text='<div class="code-cool">Bla bla blakkkskk</div>This is just some bla text. Bla bla bla.<div class="code-super">More bla here</div>More text to search here.<div class="code-cool">More bla here</div>More text to search here.';
then the output is this:
"code-cool">Bla bla blakkkskkThis is just some bla text. Bla bla bla.
More bla here
More text to search here.
Which is definitely not what I want.
Okay... Anybody that takes the challenge?