Yuval200 Posted January 8, 2007 Share Posted January 8, 2007 Hello again guys :)I have a question, lets say I have a code box, like the in IPB, how do I put numbers for each line, like:[code]1. <?php2. echo "Hello world";3. ?> I know I can use the explode, and then, the all-magical foreach. Thing is, I am looking for a solution to apply only to a PART of a string (like, the lines will apply to code boxes only, and not to the whole post). I've writen this post here because I think that the line numbering is some-what PHP related.Yuval Link to comment Share on other sites More sharing options...
aspnetguy Posted January 8, 2007 Share Posted January 8, 2007 Yes it is very much PHP related. You would need to explode your code box snippet and do the numbering in a loop. There is really no other way around it. Link to comment Share on other sites More sharing options...
Yuval200 Posted January 9, 2007 Author Share Posted January 9, 2007 Can I please have an example about it? I know how to do it with the whole string, but not with a specified part of it, as I said.. Link to comment Share on other sites More sharing options...
Guest Kiwi Posted January 9, 2007 Share Posted January 9, 2007 Can I please have an example about it? I know how to do it with the whole string, but not with a specified part of it, as I said..I assume you check on code boxes with something like this?preg_match ( '/\[code\](.+)\[\/code\]/', $text, $out ); Then you could explode on $out[1] (the index with the code), en them loop it with foreach(); like you said. Link to comment Share on other sites More sharing options...
Yuval200 Posted January 9, 2007 Author Share Posted January 9, 2007 I assume you check on code boxes with something like this?preg_match ( '/\[code\](.+)\[\/code\]/', $text, $out ); Then you could explode on $out[1] (the index with the code), en them loop it with foreach(); like you said. Thanks.. I know regex stuff, but again, can I please have an example on how to perform this? Link to comment Share on other sites More sharing options...
Escee Posted January 9, 2007 Share Posted January 9, 2007 $rules = explode ( "\n", $regex_output );foreach ( $rules as $id => $rule ){ echo $id . ': ' . $rule . '<br/>';} Link to comment Share on other sites More sharing options...
Yuval200 Posted January 9, 2007 Author Share Posted January 9, 2007 $rules = explode ( "\n", $regex_output );foreach ( $rules as $id => $rule ){ echo $id . ': ' . $rule . '<br/>';} Thanks, thing is.. $regex_output will be something like this:$regex_output = preg_replace("blabla", "blabla", $_POST['bla']) AND THAT IS WHAT I AM ASKING!How can I tell PHP that I want to explode the sub-string INSIDE the box. Please, can anyone help me. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 9, 2007 Share Posted January 9, 2007 Kiwi gave you the regex to use up there. I don't think you want to use preg_replace though, you need to use preg_match to get the contents of the box, then do your formatting, then use something like str_replace to replace the original match with the new text. Link to comment Share on other sites More sharing options...
Yuval200 Posted January 10, 2007 Author Share Posted January 10, 2007 PLEASE, can anyone give me an example with the whole thing? PLEASE Link to comment Share on other sites More sharing options...
justsomeguy Posted January 10, 2007 Share Posted January 10, 2007 You want an example of the whole thing? Is that any different then having someone else write it for you?This really isn't that difficult. The code that Kiwi gave will get the part between the code tags: preg_match ( '/\[code\](.+)\[\/code\]/', $text, $out ); Now the part that is between the tags is inside $out[1] (as Kiwi said). So, you transform $out[1] however you want to, maybe with an explode followed by a for loop, and implode it again: $lines = explode("\n", $out[1]);for ($i = 0; $i < count($lines); $i++) $lines[$i] = ($i + 1) . ". " . $lines[$i];$new = implode("\n", $lines); And then use str_replace to replace the old text with the new text. str_replace($out[1], $new, $text); Everything that I just did was already mentioned in this thread. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now