Re: [PHP] Write multiple backslashes

[email protected] (Ryan Pallas)
Newsgroups php.general
Message-ID <CAObuZdvrpz7gdgf6ESiPxZ-mwNwCNn=fmjDEuVFBLHdG3L5m2w@mail.gmail.com>
On Mar 8, 2017 6:27 AM, "Jason Pruim" <[email protected]> wrote:



Sent from my iPhone

> On Mar 8, 2017, at 4:48 AM, Danny <[email protected]> wrote:
>
> Hi,
>
> I have a file in which I need to interchange two regular expressions
every hour
> depending on what data will be processed. In other words, I will replace
the one
> regular expression with the other (they are never together in the same
file).
> This file is a configuration file for attatched hardware (not written by
myself
> but by the hardware manufacturer).
>
> Below is what it should look like in the file:
> IGNORE_TMP=^()$
> or
> IGNORE_TMP=^(\\*.*\\*)$" (Yes, this one has 2 backslashes)
>
> Now, the first regular expression I can write to the file like this with
no
> problems:
> $first_regexp = "/IGNORE_TMP=\\^\\(\\)\\$/" ;
> which will give me:
> IGNORE_TMP=^()$.
>
> However, I cannot seem to get it right to write the second regular
expression
> correctly to get 2 backlashes. I can get 1 with no problem:
> $second_regexp = "IGNORE_TMP=^(\\*.*\\*)$" ;

I'm no regard expert but don't you have to escape it for each one?
Something like $second_rexexp = "ignore_tmp=^(\\\\*.*\\\\*)$";

I could be way off base though...


Yes that's true but only because it is a double quoted string, meaning
backslashes are treated as escape sequences.

You could also just use a single quoted string.

$second_regexp = 'IGNORE_TMP=^(\\*.*\\*)$';



> ### Here is just the rest of the code for reference ###
> $block_file = file_get_contents ( $tmp_file ) ;
> $block_regexp = preg_replace ( $old_block, $new_block, $block_file ) ;
> file_put_contents ( $tmp_file, $block_regexp ) ;
> $remove_blank_lines = exec ( 'sed -i "/^[[:space:]]*$/d"
/etc/scripts/motor_control/temperature.conf' ) ;
> #######################################################
>
> Any pointers?
>
> Thank you
>
> Danny
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.