| | |
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 |
advanced apache api array basics beginner binary broken cakephp checkbox class cms code codingproblem combobox cookies cron curl database date datepart display dynamic echo email error file files folder form forms function functions google head href htaccess html image include includingmysecondfileinthechain insert ip javascript job joomla js limit link login mail menu mlm mobile multiple mysql oop outofmemmory paging parse password paypal pdf php problem procedure query radio random recursion remote return script search server sessions smarty sms source space sql stored syntax system table traffic tutorial unicode up-to-date update upload url validation validator variable video web webapplications websitecontactform youtube






