|
|
preg_split help
|
|
|
|
Grizzled Veteran
Join Date: Oct 2003
Status:
Offline
|
|
I have a bunch of "language" files with constant defines in them. I'm trying to build an editor to edit them from the web so I don't have to keep editing them and FTPing them up every time I want to change some text.
Anyways, I've managed to mostly split the files but I'm having a small problem.
Here is a test example:
Code:
*damn php BB code stripping out slashes*
<?php
$test_file = "<?php
define('HEADING_TITLE', 'Contact Us');
define('NAVBAR_TITLE', 'Contact Us');
define('TEXT_SUCCESS', 'Your enquiry has been successfully sent.');
define('EMAIL_SUBJECT', 'Enquiry from ' . STORE_NAME . '');
define('TEXT_SELECT_DEPARTMENT', '
Please select a department you would like to get in contact with:<br />
We would like that
');
?>";
$split = preg_split('/define\(\'(.*)\', \'(.*)\'\);\\n/', $test_file, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
echo "<pre>";
print_r($split);
echo "</pre>";
?>
The 1st 4 lines work as expected, but the TEXT_SELECT_DEPARTMENT does not. The problem is the new lines within the define. Some will have this and some won't. I am trying to match between "define('" and "', '" and "');" to get the constants and their value for the editor.
I am almost there and what I have so far output the following:
Code:
Array
(
[0] => <?php
[1] => HEADING_TITLE
[2] => Contact Us
[3] => NAVBAR_TITLE
[4] => Contact Us
[5] => TEXT_SUCCESS
[6] => Your enquiry has been successfully sent.
[7] => EMAIL_SUBJECT
[8] => Enquiry from ' . STORE_NAME . '
[9] =>
define('TEXT_SELECT_DEPARTMENT', '
Please select a department you would like to get in contact with:<br />
We would like that
');
?>
)
can anyone help me get it to work with new lines inside of defines as well?
(
Last edited by madmacgames; Aug 21, 2004 at 09:12 PM.
)
|
The only thing necessary for evil to flourish is for good men to do nothing
- Edmund Burke
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Oct 2003
Status:
Offline
|
|
I *think* I have got it. Well this is not the prettiest but it seems to work, if anyone is interested:
Code:
$split = preg_split('/define\(\'(.*)\', \'|\', \'\\n(.*)\\n\'\);|\'\);\\n/', $test_file, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
|
The only thing necessary for evil to flourish is for good men to do nothing
- Edmund Burke
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status:
Offline
|
|
Is there a reason you used PREG_SPLIT?
I used PREG_MATCH_ALL and it returns an array of the matches, rather than all the extra unneeded data:-
[php]
preg_match_all("/^define\('(.*?)',\\s+'(.*?)'\);\\s+/msi", $test_file, $split, PREG_SET_ORDER);
[/php]
Returns:
Code:
Array
(
[0] => Array
(
[0] => define('HEADING_TITLE', 'Contact Us');
[1] => define('NAVBAR_TITLE', 'Contact Us');
[2] => define('TEXT_SUCCESS', 'Your enquiry has been successfully sent.');
[3] => define('EMAIL_SUBJECT', 'Enquiry from ' . STORE_NAME . '');
[4] => define('TEXT_SELECT_DEPARTMENT', '
Please select a department you would like to get in contact with:
We would like that
');
)
[1] => Array
(
[0] => HEADING_TITLE
[1] => NAVBAR_TITLE
[2] => TEXT_SUCCESS
[3] => EMAIL_SUBJECT
[4] => TEXT_SELECT_DEPARTMENT
)
[2] => Array
(
[0] => Contact Us
[1] => Contact Us
[2] => Your enquiry has been successfully sent.
[3] => Enquiry from ' . STORE_NAME . '
[4] =>
Please select a department you would like to get in contact with:
We would like that
)
)
|
Computer thez nohhh...
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Oct 2003
Status:
Offline
|
|
Originally posted by Simon Mundy:
Is there a reason you used PREG_SPLIT?
I used PREG_MATCH_ALL and it returns an array of the matches, rather than all the extra unneeded data
I briefly looked over the preg functions and must have looked over the fact that preg_match_all will return an array of matches. You are right it is much easier than preg_split, which was a pain to get to work right. lol
|
The only thing necessary for evil to flourish is for good men to do nothing
- Edmund Burke
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|