Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

strip out the spaces

Posted on 2013-01-26
5
221 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
ID: 38822876
If you want to erase all spaces, just do it:
$phone_content_raw = str_replace(' ', '', $phone_content_raw);
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 38822878
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
ID: 38822890
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 31

Expert Comment

by:Marco Gasi
ID: 38822904
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
ID: 38822936
this solution was the easiest to understand
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

791 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