Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > preg_split help

preg_split help
Thread Tools
madmacgames
Grizzled Veteran
Join Date: Oct 2003
Status: Offline
Reply With Quote
Aug 21, 2004, 05:43 PM
 
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
     
madmacgames  (op)
Grizzled Veteran
Join Date: Oct 2003
Status: Offline
Reply With Quote
Aug 21, 2004, 09:10 PM
 
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
     
Simon Mundy
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Aug 22, 2004, 07:09 PM
 
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...
     
madmacgames  (op)
Grizzled Veteran
Join Date: Oct 2003
Status: Offline
Reply With Quote
Aug 23, 2004, 07:02 PM
 
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 Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 09:08 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,