Regex help

Reply

Join Date: Oct 2006
Posts: 164
Reputation: Barefootsanders is an unknown quantity at this point 
Solved Threads: 3
Barefootsanders Barefootsanders is offline Offline
Junior Poster

Regex help

 
0
  #1
Jul 3rd, 2009
Hi all.. .I'm trying to use preg_split but I'm having trouble getting the regular expression I want.

What I have now is a long character string, upwards of 200 characters depending on user input. I'm trying to do is break up my string an array, each element in that array having 50 characters. But I dont want to use str_split because that has the possibility to break up the string in the middle of words. So I'd like to create a regular expression that will get at max 50 characters and end in a space and use the functionality of preg_split to accomplish this.

What my pattern looks like is this (and mind you I'm not all that good with regex...)
  1. /.{1,5}(?=\s*)/
Like I said, should be any string 1-50 characters long with a space proceeding the last character.

Any help would be much appreciated.

Thanks!
Last edited by Barefootsanders; Jul 3rd, 2009 at 4:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,385
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 217
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Regex help

 
0
  #2
Jul 3rd, 2009
Post an example string that is to be split, guessing based on your description will produce bad results
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,225
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 166
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Regex help

 
0
  #3
Jul 3rd, 2009
I wouldn't use a regular expression for something like this. I am not the best with it and a php function to do it wouldn't be hard.

  1. function splitString( $data,$len=50 ) {
  2. $data = explode( ' ',$data );
  3. $result = array();
  4. $i = 0;
  5. foreach( $data as $str ) {
  6. $i = ( $i + strlen( $str ) + 1 );
  7. if ( $i <= ( $len - 1 ) ) {
  8. $result[] = $str;
  9. }
  10. }
  11. return implode( ' ',$result ) . ' ';
  12. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC