Re: [PHP] preg_split problem

2004-03-11 Thread Jason Wong
On Wednesday 10 March 2004 19:53, [EMAIL PROTECTED] wrote:
 I have a problem separting a text string with the preg_split function
 If somebody could help, it would be greatly appreciated.
 -
 The problem I have is that my code, so far, does the job, except it
 recognises ALL : and not only those following PAGE.
 This should be fairly easy to solve?
 -
 What I want to do:

 As part of a php-controlled content management system, this code has the
 purpose to link to other page within the same website.
 Example: See also this page.
 When editing content, the administrator should be able to perform this link
 by editing the string above as:
 See also PAGE:15:this page:PAGE., so containing:
 PAGE: (opening tag, to be found by php code to insert a a href...)
 15: number of the corresponding page

 :PAGE (end tag to be replaced by /a)

The following will only work when the string contains a single link:

$doo = See also PAGE:15:this page:PAGE.;
$dah = preg_replace(|(.*)PAGE:(\d+):(.*):PAGE(.*)|U, $1a 
href=\main.php?id=$2\$3/a$4, $doo);
echo $doobr$dah;

Season to taste.

-- 
Jason Wong - Gremlins Associates - www.gremlins.biz
Open Source Software Systems Integrators
* Web Design  Hosting * Internet  Intranet Applications Development *
--
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
--
/*
Victory or defeat!
*/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_split problem

2002-11-09 Thread Marek Kilimajer
list throws away the other two array elements, you need
list($one,$two,$three,$rest) = preg_split(/\s+/, $text, 4);



samug wrote:


Could someone tell me why isn't this working?

$text = And what did I tell you?;
list($one,$two,$three,$rest) = preg_split(/\s+/, $text);
print($rest);

The result would be I and not I tell you?
Why?



 



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] preg_split problem

2002-11-09 Thread Jason Wong
On Sunday 10 November 2002 00:58, samug wrote:
 Could someone tell me why isn't this working?

 $text = And what did I tell you?;
 list($one,$two,$three,$rest) = preg_split(/\s+/, $text);
 print($rest);

 The result would be I and not I tell you?
 Why?

Because that is what you asked for!

$doo = preg_split(/\s+/, $text);
print_r($doo); // each word is a separate element in $doo.

In your list() you're only taking up to the 4th element which is I.

-- 
Jason Wong - Gremlins Associates - www.gremlins.com.hk
Open Source Software Systems Integrators
* Web Design  Hosting * Internet  Intranet Applications Development *

/*
Something unpleasant is coming when men are anxious to tell the truth.
-- Benjamin Disraeli
*/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] preg_split problem

2002-11-09 Thread Ernest E Vogelsinger
At 17:58 09.11.2002, samug said:
[snip]
$text = And what did I tell you?;
list($one,$two,$three,$rest) = preg_split(/\s+/, $text);
print($rest);

The result would be I and not I tell you?
[snip] 

Your regex splits the entire string, and since you have only a list of four
elements, the fourth element happens to be the I.

If you want the rest in $rest, you need a thried parameter to preg_split
telling it how many elements the resulting array should have:

$text = And what did I tell you?; 
list($one,$two,$three,$rest) = preg_split(/\s+/, $text, 4); 
print($rest);

will correctly output I tell you?


-- 
   O Ernest E. Vogelsinger
   (\)ICQ #13394035
^ http://www.vogelsinger.at/



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php