Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to search text for "code-*" and return all occurences of words

Status
Not open for further replies.

Tore

Technical User
May 18, 2001
29
NO
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?



 
Hi got the answer from the previous forum I tried:
Here it is if you need it:

$string_test='<div class="code-div" style="margin: 0pt 20px 0pt 0pt; float: left;">Bla bla blakkkskk</div>This is just some bla text. Bla bla bla.<div style="margin: 0pt 20px 0pt 0pt; float: left;" class="code-super" style="margin: 0pt 20px 0pt 0pt; float: left;">More bla here</div>More text to search here.<div class="code-cool">More bla here</div>More text to search here.';

$pattern='/<div.*?class="(code-.+?)".*?>/';


preg_match_all($pattern,$string_test,$matches);

echo '<pre>';
print_r($matches);
echo '</pre>';

$matches=$matches[1];
$matches_unique=array_unique($matches);//remove duplicates

echo $matches_string=implode(', ',$matches_unique);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top