Jump to content

How to number lines of code in PARTS of a string?


Yuval200

Recommended Posts

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

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

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

$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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...