?
Solved

Remove words with numbers from a long string

Posted on 2009-02-16
5
Medium Priority
?
247 Views
Last Modified: 2012-06-21
Hi, I have a string and inside this string I collect some phrases like:
$string = "holiday$ in portugal, algarve villa, alga1111rve holidays, algarve villa, villas in portugal,";
I want to remove the words like holiday$ and alga1111rve, and after I remove that words I get the string again preserving the commas(,), like:
$string = "in portugal, algarve villa, holidays, algarve villa, villas in portugal,";

Inside snipped code, you can see my script, but in final $coleta_keywords_final is empty!!!!!
Attention:  $coleta_keywords_final, the string have allways data formated like "in portugal, algarve villa, holidays, algarve villa, villas in portugal,".

What I do wrong?

Regards, JC
$keep = explode(',', $coleta_keywords_final); 
$coleta_keywords_final = implode(" , ", $keep);
$words = explode(',',$coleta_keywords_final);
$result = array();
foreach($words as $word)
if(!eregi('[^a-z]',$word) or 
!eregi('[^0-9]',$word)) 
$result[]=$word;
$coleta_keywords_final = implode(',',$result);
echo "<br> $coleta_keywords_final <br>";

Open in new window

0
Comment
Question by:Pedro Chagas
  • 3
  • 2
5 Comments
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 23652201
Somethimes the string can be too:
$string = "holiday$ in portugal, villa, alga1111rve holidays, algarve villa, villas in portugal, sun, black";
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 23652408
For help:
The variable $coleta_keywords_final contains "holiday$ in portugal, villa, alga1111rve holidays, algarve villa, villas in portugal, sun, black"
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 23654685
You can look at each word selectively.  I will post an example that does this.

Best, ~Ray
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 23654709
Ray, do you have the link for that post?

Regards, JC
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 23654749

<?php // RAY_temp_editwords.php
 
// SOME TEST DATA
$raw_data = "holiday$ in portugal, villa, alga1111rve holidays, algarve villa, villas in portugal, sun, black";
 
// MAKE SURE THERE ARE NOT REPLICATED BLANKS
$my_data = trim(ereg_replace(' +', ' ', $raw_data));
 
// BREAK INTO ARRAY ON BLANKS
$array = explode(' ', $my_data);
 
// CREATE AN OUTPUT STRING
$out_data = '';
 
// ITERATE OVER THE ARRAY OF WORDS
foreach ($array as $word)
{
 
// APPLY ANY TESTS YOU WANT TO THE WORDS HERE
   if (ereg('\$', $word))    continue; // SKIP WORD WITH DOLLAR SIGNS
   if (ereg('[0-9]', $word)) continue; // SKIP WORDS WITH NUMBERS
 
// PUT ACCEPTABLE WORDS INTO THE OUTPUT STRING
   $out_data .= $word . ' ';
}
 
// TRIM OFF THE TRAILING NOISE
$out_data = rtrim($out_data, ' ');
 
var_dump($out_data);

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question