![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Apr 2005
Posts: 126
Rep Power: 4
![]() |
Preg_replace giving me hell!
Alright im working on a content management system, the templates use custom input boxes and are stored in the page, I have a couple of arrays of known values that have the values of data transfered from the first page, but the tricky part is putting them into the template in the correct spot keeping whats needed... mmm, so anyways heres whats not working:
$rtemplate = preg_replace("/\[\{box,([a-zA-Z0-9]*),([0-9]*),([0-9]*)\}\]/",$custvarv['\\1'], $rtemplate);Its supposed to change [{box,ccontent,40,10}] to $custvarv[ccontent], simple right? no, it just changes it to nothing, ive tried changing the replacement to "custvarv['\\1']" and whata you know, custvarv[ccontent] perfect, ive also printed out $custvarv[ccontent] right after it to make sure its not empty, and its not. I could loop through a for statement for each input and have the preg, actually 2, 1 for box and 1 for input, for each input thing, which seems like a bad idea, so any help on this? the code I pasted just replaces the text with nothing, but does not return any errors. Please help, google has returned me nothing useful. |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I'm not sure what you want to do - that doesn't even look like a valid regex to me. Can you post the stuff you want to replace, and what you want the end result to be?
|
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I second Ooble's motion -- post examples of the transformation you'd like to see. Regex, while very effective, is a resource hog and tends to be overused. Simple substitutions are sometimes more effective.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Apr 2005
Posts: 126
Rep Power: 4
![]() |
How else can I do this? This isnt done at runtime, the templates are designed and stored in the database for users convinionce and to avoid complicating them with html, and if I was to convert the forms on the template from [box, nodns,ewmfds] stuff like that and converted it to its textarea to be stored in the database, there would be no way to replace the correct values which will have to be done no matter what. Since my code is huge, and it doesnt matter what im replacing, thats not the problem in my code $array[\\1] is not possible, how do I do that? that is all my question is, the preg_replace is fine, its running beutifuly but $array[\\1] does not work, its not the correct syntax to use it, if its possible, im trying to fill the \\1 as it would any other time, the first data between ( and ) in the preg_statement, but instead I got an error, so I had to go $array['\\1'] but still nothing, worked, but replaced it with nothing.
I know its a bad idea to go overboard with preg_replace but in my case its needed a lot. But atleast this is all done before the user looks at the page, the majority of the pregs_ are done only when pages are inserted, modified, and templates are inserted and modified, very few done when the page loads. So to recap: - Whats valid regex? - The code I have DOES work, just somethings not right - I want [{box,ccontent,40,10}] to be replaced with $custvarv[\\1] whatever \\1 may be $rtemplate = preg_replace("/\[\{box,([a-zA-Z0-9]*),([0-9]*),([0-9]*)\}\]/",$custvarv['\\1'], $rtemplate); Problem is with $custvarv['\\1'] its not replacing it with the value of $custvarv[ccontent] like it should, when it pregs through [{box,ccontent,40,10}]. That is my question, how do I use the array like that at the end? |
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Ah... I see. What you need to do is return "\\1", saving that in $rtemplate. Then copy the value of $custvarv[$rtemplate] into $rtemplate.
[php]$rtemplate = preg_replace("/\[\{box,([a-zA-Z0-9]*),([0-9]*),([0-9]*)\}\]/", '\\1', $rtemplate); $rtemplate = $custvarv[$rtemplate];[/php] |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Apr 2005
Posts: 126
Rep Power: 4
![]() |
mmm, im trying to replace 1 value in it, $rtemplate will look like this:
[{pagetitle}] added on [{pagedate}] by [{author}]
[{box,ccontent,40,10}]
[{box,cfooter,40,10}]
[{box,cclosing,40,10}]
Click to return homeMost of that is just testing, because i need to get the template to work in any situation with any amount of boxes, text, and inputs... I cant change the whole things to 1 value... any way I can do this? Because my array is set up $custvarv[NAMEOFFIELD] its easy to access and get its value if I was able to plug in the name of the box directly, without going through anything else, otherwise I have to do this: for ($i =0; $i<sizeof($custvarn[$i]); $i++) {
$rtemplate = preg_replace("/\[\{box,$custvarn[$i],[0-9]*,[0-9]*\}\]/",$custvarv[$custvarn[$i]], $rtemplate);
}which does work, but I dont like how it searches through it over and over for each custom value, there could be 10+ existing on the more complex templates... so yea... |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|