| | |
Regex help
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2006
Posts: 164
Reputation:
Solved Threads: 3
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...)
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!
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...)
PHP Syntax (Toggle Plain Text)
/.{1,5}(?=\s*)/
Any help would be much appreciated.
Thanks!
Last edited by Barefootsanders; Jul 3rd, 2009 at 4:35 pm.
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.
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.
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.
PHP Syntax (Toggle Plain Text)
function splitString( $data,$len=50 ) { $data = explode( ' ',$data ); $result = array(); $i = 0; foreach( $data as $str ) { $i = ( $i + strlen( $str ) + 1 ); if ( $i <= ( $len - 1 ) ) { $result[] = $str; } } return implode( ' ',$result ) . ' '; }
![]() |
Similar Threads
- Matching multiple href="Something.aspx" using RegEx (ASP.NET)
- regex question (Perl)
- regex (boost) (C++)
- Getting the start / end of string in regex through match objects (Python)
- Regex in Java (Java)
- Regex for password (Shell Scripting)
- Regex in C++ (C++)
- help with regex...and marking up text in JTextpane with html (Java)
Other Threads in the PHP Forum
- Previous Thread: T_STRING error (line 183) - NextGen Gallery Plugin
- Next Thread: Field List Input Problem
| Thread Tools | Search this Thread |
apache api array basic beginner binary body broken cakephp class cms code computing confirm cron curl customizableitems database date date/time delete display dynamic echo email error file files filter folder form forms forum function functions gc_maxlifetime global google headmethod href htaccess html iframe image include ip javascript joomla limit link list login malfunction memmory memory menu mlm msqli_multi_query multiple mycodeisbad mysql navigation oop parameter parsing paypal pdf php phpmysql query question random recourse recursion regex script search select seo server sessions snippet source space sql static system table thesishelp trouble tutorial update upload url variable video web webdesign xml youtube






