Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

strip out the spaces

Posted on 2013-01-26
5
Medium Priority
?
225 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

715 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