Solved

strip out the spaces

Posted on 2013-01-26
5
222 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

679 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