Jump to content

Prevent Arrays With preg_match() function


mojobadshah

Recommended Posts

I would like to be able to use a succession of php preg_match() and preg_replace() functions in one script without having to produce clutter in this match and replace php coding script and without its results showing arrayed forms. The following is a RegExp formula that I'm comfortable with that involves only a replace function. I'm curious what the best way would be to scoot a RegExp match function into a script like this one that is consistent with the same $current .= preg_replace('/a/', 'b', $current); php code that I've shown here et. al. that can be written out on one string. I've seen some php implode()  functions, but my concern is that an additional function as such along with preg_mach() functions will cause a lot of deviation in a list of regular regex match and replace functions. What can I do to keep both php match and replace RegExp functions strings typed out in linear concession for the sake of keeping things organized when strictly relating to regex functions and working with them within php and working something like an implode() function into my php script so that match and replace functions can produced organized results outside of having to make use of arrays?       

<?php
$file = 'sample.txt';
$current = file_get_contents($file);
$current .= preg_replace('/a/', 'b', $current);
file_put_contents($file, $current);
?>

 

Edited by mojobadshah
Link to comment
Share on other sites

What?

The code you posted will take sample.txt, read what's in it, add to that another copy of what's in it but with the regex run on it, then dump that all back to sample.txt. So if you run the code multiple times then the files goes like

a -> ab -> abbb -> abbbbbbb -> abbbbbbbbbbbbbbb...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.