| | |
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
Views: 211 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl customizableitems database date directory display download dynamic echo email error file files folder form format forms forum function functions google headmethod href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail malfunctioning menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql structure syntax system table tutorial update updates upload url validation validator variable video web xml youtube






