Solved

strip out the spaces

Posted on 2013-01-26
5
223 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
[X]
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
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

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…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
The viewer will learn how to dynamically set the form action using jQuery.

752 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