Viewed   80 times

I need a replace string once function and believe preg_match might be my best bet.

I was using this, but due to the dynamicness of use, sometimes this function behaves strangely:

function str_replace_once($remove , $replace , $string)
{
    $pos = strpos($string, $remove);
    if ($pos === false) 
    {
    // Nothing found
    return $string;
    }
    return substr_replace($string, $replace, $pos, strlen($remove));
} 

Now I am taking this approach but have ran to to the error listed below.... I'm parsing all kinds of html strings with this function, so its hard to give a value thats causing the error. As of now 80% of my uses of the below show this error .

function str_replace_once($remove , $replace , $string)
{
    $remove = str_replace('/','/',$remove);
    $return = preg_replace("/$remove/", $replace, $string, 1);  
    return $return;
}  

error:

Warning: preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 0

Can anyone refine a solution?

 Answers

5

You are looking for preg_quote instead of trying to escape the yourself (which doesn't take [, + and many others into account):

$return = preg_replace('/'.preg_quote($remove,'/').'/', $replace, $string, 1);
Tuesday, September 6, 2022
5

It depends on your case: if you're trying to do something fairly basic (eg: search for a string, replace a substring with something else), then the regular string functions are the way to go. If you want to do something more complicated (eg: search for IP addresses), then the Regex functions are definitely a better choice.

I haven't profiled regexes so I can't say that they'll be faster at runtime, but I can tell you that the extra time spent hacking together the equivalent using the basic functions wouldn't be worth it.


Edit with the new information in the OP:

It sounds as though you actually need to do a number of small string operations here. Since each one individually is quite basic, and I doubt you'd be able to do all those steps (or even a couple of those steps) at one time using a regex, I'd go with the basic functions:

Grab the first half (based on the third location of a substring "000000") and compare its hash to the next 20 bytes, throwing away anything left.

Use: strpos() and substr()
Or : /$(.*?0{6}.*?0{6}.*?)0{6}/

Then grab the next 19 bytes after that, and split that into 8 (toss 1) and 8.

Use: substr() - (I assume you mean 17 bytes here -- 8 + 1 + 8)

$part1 = substr($myStr, $currPos, 8);
$part2 = substr($myStr, $currPos + 9, 8);
Friday, December 2, 2022
4
(19|20)[0-9][0-9]

This will read in only 1900 and 2000 ranged dates.

Monday, December 5, 2022
 
jarno
 
2

To check if text contains some substring only once, you need to check match all characters that do not constitute <scr>, then match <scr>, and use a negative look-ahead to check if there is no <scr> further, and consume all characters. Also, line/string boundaries ^/$ are a must:

^(?:(?!<scr>).)*<scr>(?!.*<scr>).*$

See demo

EXPLANATION:

  • ^ - Start of line (as m multiline option is ON)
  • (?:(?!<scr>).)* - A non-capturing group to match each character (not a newline - for that, you need to also add s singleline option) that is not preceded with <scr>
  • <scr> - Our literal <scr>
  • (?!.*<scr>) - The negative lookbehind checking that we do not have <scr> any more
  • .*$ - Subpattern matching the rest of the characters to the end of the line.
Thursday, November 10, 2022
3

Wrangled with this for a while today and running migrations in conjunction with migrations seems to do the trick. A snapshot of my test is as follows:

<?php

namespace TestsBrowser;

use AppUser;
use TestsDuskTestCase;

use IlluminateFoundationTestingDatabaseMigrations;
use IlluminateFoundationTestingDatabaseTransactions;

class DefaultTest extends DuskTestCase
{
    use DatabaseMigrations, DatabaseTransactions;

    /**
     * A Dusk test example.
     *
     * @return void
     */
    public function test_something()
    {
        //Add test stuff here
    }
}

I've got a couple of factories in my actual test and they seem to run through the migrations with the data destroyed after the test as expected.

Wednesday, August 10, 2022
 
geun
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :