Solved

strip out the spaces

Posted on 2013-01-26
5
215 Views
Last Modified: 2013-01-26
    $phone_content_raw = str_replace(' one ', 'one', $phone_content_raw);
    $phone_content_raw = str_replace(' two ', 'two', $phone_content_raw);
    $phone_content_raw = str_replace(' three ', 'three', $phone_content_raw);
    $phone_content_raw = str_replace(' four ', 'four', $phone_content_raw);
    $phone_content_raw = str_replace(' five ', 'five', $phone_content_raw);
    $phone_content_raw = str_replace(' six ', 'six', $phone_content_raw);
    $phone_content_raw = str_replace(' seven ', 'seven', $phone_content_raw);
    $phone_content_raw = str_replace(' eight ', 'eight', $phone_content_raw);
    $phone_content_raw = str_replace(' nine ', 'nine', $phone_content_raw);
    $phone_content_raw = str_replace(' zero ', 'zero', $phone_content_raw);
  
    
    $phone_content_raw = str_replace('one', '1', $phone_content_raw);
    $phone_content_raw = str_replace('two', '2', $phone_content_raw);
    $phone_content_raw = str_replace('three', '3', $phone_content_raw);
    $phone_content_raw = str_replace('four', '4', $phone_content_raw);
    $phone_content_raw = str_replace('five', '5', $phone_content_raw);
    $phone_content_raw = str_replace('six', '6', $phone_content_raw);
    $phone_content_raw = str_replace('seven', '7', $phone_content_raw);
    $phone_content_raw = str_replace('eight', '8', $phone_content_raw);
    $phone_content_raw = str_replace('nine', '9', $phone_content_raw);
    $phone_content_raw = str_replace('zero', '0', $phone_content_raw);

Open in new window


this code takes numbers out of a block of text

this code is replacing ' words '  and 'words' with numbers

is there a way to do strip out the spaces to do this code on less lines
0
Comment
Question by:rgb192
  • 2
  • 2
5 Comments
 
LVL 10

Expert Comment

by:acbxyz
Comment Utility
If you want to erase all spaces, just do it:
$phone_content_raw = str_replace(' ', '', $phone_content_raw);
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
Use this:

    $phone_content_raw = str_replace(trim(' one '), 'one', $phone_content_raw);
    $phone_content_raw = str_replace(trim(' two '), 'two', $phone_content_raw);
    $phone_content_raw = str_replace(trim(' three '), 'three', $phone_content_raw);
    $phone_content_raw = str_replace(trim(' four '), 'four', $phone_content_raw);
    $phone_content_raw = str_replace(trim(' five '), 'five', $phone_content_raw);
    $phone_content_raw = str_replace(trim(' six '), 'six', $phone_content_raw);
    $phone_content_raw = str_replace(trim(' seven '), 'seven', $phone_content_raw);
    $phone_content_raw = str_replace(trim(' eight '), 'eight', $phone_content_raw);
    $phone_content_raw = str_replace(trim(' nine '), 'nine', $phone_content_raw);
    $phone_content_raw = str_replacetrim((' zero '), 'zero', $phone_content_raw);

Open in new window

0
 
LVL 10

Accepted Solution

by:
acbxyz earned 500 total points
Comment Utility
The solution of marqusG does not work.
trim(' one ') = 'one'

Open in new window


If you need the same functionality as shown above the only way to make it shorter is a foreach:
foreach (array('one' => 1, 'two' => 2 'three' => 3, 'four' => 4, 'five' => 5, 'six' => 6, 'seven' => 7, 'eight' => 8, 'nine' => 9, 'zero' => 0) as $text => $number) {
    $phone_content_raw = str_replace(' ' . $text . ' ', $number, $phone_content_raw);
    $phone_content_raw = str_replace($text, $number, $phone_content_raw);
}

Open in new window

0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
Well, @acbxyz, you're right, I did an error: my intended solution was:

    $phone_content_raw = str_replace(trim(' one '), '1', $phone_content_raw);
    $phone_content_raw = str_replace(trim(' two '), '2', $phone_content_raw);
    $phone_content_raw = str_replace(trim(' three '), '3', $phone_content_raw);
    $phone_content_raw = str_replace(trim(' four '), '4', $phone_content_raw);
    $phone_content_raw = str_replace(trim(' five '), '5', $phone_content_raw);
    $phone_content_raw = str_replace(trim(' six '), '6', $phone_content_raw);
    $phone_content_raw = str_replace(trim(' seven '), '7', $phone_content_raw);
    $phone_content_raw = str_replace(trim(' eight '), '8', $phone_content_raw);
    $phone_content_raw = str_replace(trim(' nine '), '9', $phone_content_raw);
    $phone_content_raw = str_replacetrim((' zero '), '0', $phone_content_raw);

Open in new window


But i agree your solution is better :-)
0
 

Author Closing Comment

by:rgb192
Comment Utility
this solution was the easiest to understand
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now