FAQ
Hello;
I have the following code:

$prps = str_replace("\n", ' ', $input[3]);
$request = str_replace("// var purpose = {} ;\n", "var purpose =
'$prps';\n", $request);

In the first line $input[3] is a string formatted with new lines at the
end of each line.
It is to be used to initialize a javascript variable (in the second
line above), in an html file
template.
When the html file is generated from the template, the javascript
written to it fails
with unterminated string literal error message.
When opening a view source window, the 'var purpose...' line does have
the string
broken up with extra spaces at the beginning of each line. This
indicates that
the new line was not replaced with the space. The space was merely
added after
the new line.
How do you replace a new line with php in a case like this?

Testing this is very tedious. Each time I have to go through and undo
file modifications that this function performs in addition to the above.
So it is not just a case of making a change and reloading the file
and rerunning it.

Thanks in advance;
JK

Search Discussions

  • Greg Donald at Jul 2, 2007 at 10:15 pm

    On 6/30/07, jekillen wrote:
    Hello;
    I have the following code:

    $prps = str_replace("\n", ' ', $input[3]);
    Are you sure $input[3] doesn't have two newlines at the end?

    Why not use trim() to be sure?
    $request = str_replace("// var purpose = {} ;\n", "var purpose =
    '$prps';\n", $request);

    In the first line $input[3] is a string formatted with new lines at the
    end of each line.
    It is to be used to initialize a javascript variable (in the second
    line above), in an html file
    template.
    When the html file is generated from the template, the javascript
    written to it fails
    with unterminated string literal error message.
    When opening a view source window, the 'var purpose...' line does have
    the string
    broken up with extra spaces at the beginning of each line. This
    indicates that
    the new line was not replaced with the space. The space was merely
    added after
    the new line.
    How do you replace a new line with php in a case like this?
    Why do you want to replace it, you just said you wanted to remove it above?
    Testing this is very tedious.
    Do you use Firebug? It's a Firefox extension. Makes debugging
    Javascript very easy.

    Also you might try the 'view rendered source' extension. Lets you
    view the live DOM, not just the possibly outdated version of your html
    the view source option gives.
    Each time I have to go through and undo
    file modifications that this function performs in addition to the above.
    So it is not just a case of making a change and reloading the file
    and rerunning it.
    Brutal. :(
  • Jekillen at Jul 3, 2007 at 1:05 am
    On Jul 2, 2007, at 6:07 PM, jekillen wrote:
    On Jul 2, 2007, at 3:15 PM, Greg Donald wrote:
    On 6/30/07, jekillen wrote:
    Hello;
    I have the following code:

    $prps = str_replace("\n", ' ', $input[3]);
    Are you sure $input[3] doesn't have two newlines at the end?

    Why not use trim() to be sure?
    $request = str_replace("// var purpose = {} ;\n", "var purpose =
    '$prps';\n", $request);

    In the first line $input[3] is a string formatted with new lines at
    the
    end of each line.
    It is to be used to initialize a javascript variable (in the second
    line above), in an html file
    template.
    When the html file is generated from the template, the javascript
    written to it fails
    with unterminated string literal error message.
    When opening a view source window, the 'var purpose...' line does
    have
    the string
    broken up with extra spaces at the beginning of each line. This
    indicates that
    the new line was not replaced with the space. The space was merely
    added after
    the new line.
    How do you replace a new line with php in a case like this?
    Why do you want to replace it, you just said you wanted to remove it
    above?
    So it will not cause javascript errors when written as a javascript
    variable value
    The out put goes like this:
    var string = 'this is some text(\n)
    with a new line in the string.'
    It cause a javascript unterminated string literal error because of the
    line break
    caused by the new line. I need to replace the new lines with spaces so
    it is
    one long string.
    It is that simple, Please.....
    Do you use Firebug? It's a Firefox extension.
    I do not need to debug the javascript, It is the php which
    is tedious to change and retest. The php code writes
    the javascript to an html file, So I need to change the php
    or find another way.

    Does anyone have a simple answer to a simple question?
    I am trying to replace newlines in a string with spaces. The code
    I posted originally is not working. Perhaps I am doing something
    wrong in that code,
    That is all I am asking.
    JK
  • Jim Lucas at Jul 3, 2007 at 2:57 am

    jekillen wrote:
    On Jul 2, 2007, at 6:07 PM, jekillen wrote:

    On Jul 2, 2007, at 3:15 PM, Greg Donald wrote:
    On 6/30/07, jekillen wrote:
    Hello;
    I have the following code:

    $prps = str_replace("\n", ' ', $input[3]);
    Are you sure $input[3] doesn't have two newlines at the end?

    Why not use trim() to be sure?
    $request = str_replace("// var purpose = {} ;\n", "var purpose =
    '$prps';\n", $request);

    In the first line $input[3] is a string formatted with new lines at the
    end of each line.
    It is to be used to initialize a javascript variable (in the second
    line above), in an html file
    template.
    When the html file is generated from the template, the javascript
    written to it fails
    with unterminated string literal error message.
    When opening a view source window, the 'var purpose...' line does have
    the string
    broken up with extra spaces at the beginning of each line. This
    indicates that
    the new line was not replaced with the space. The space was merely
    added after
    the new line.
    How do you replace a new line with php in a case like this?
    Why do you want to replace it, you just said you wanted to remove it
    above?
    So it will not cause javascript errors when written as a javascript
    variable value
    The out put goes like this:
    var string = 'this is some text(\n)
    with a new line in the string.'
    It cause a javascript unterminated string literal error because of the
    line break
    caused by the new line. I need to replace the new lines with spaces so
    it is
    one long string.
    It is that simple, Please.....
    Do you use Firebug? It's a Firefox extension.
    I do not need to debug the javascript, It is the php which
    is tedious to change and retest. The php code writes
    the javascript to an html file, So I need to change the php
    or find another way.

    Does anyone have a simple answer to a simple question?
    I am trying to replace newlines in a string with spaces. The code
    I posted originally is not working. Perhaps I am doing something
    wrong in that code,
    That is all I am asking.
    JK
    $out = str_replace("\n", '\n', $in);

    This will actually have a \n show in the javascript, so when it is
    printed/used by JS it will have a line feed in the output of JS.
  • Richard Lynch at Jul 3, 2007 at 5:38 am

    On Sat, June 30, 2007 12:04 pm, jekillen wrote:
    Hello;
    I have the following code:

    $prps = str_replace("\n", ' ', $input[3]);
    $request = str_replace("// var purpose = {} ;\n", "var purpose =
    '$prps';\n", $request);

    In the first line $input[3] is a string formatted with new lines at
    the
    end of each line.
    It is to be used to initialize a javascript variable (in the second
    line above), in an html file
    template.
    When the html file is generated from the template, the javascript
    written to it fails
    with unterminated string literal error message.
    When opening a view source window, the 'var purpose...' line does have
    the string
    broken up with extra spaces at the beginning of each line. This
    indicates that
    the new line was not replaced with the space. The space was merely
    added after
    the new line.
    How do you replace a new line with php in a case like this?
    I don't know what you think you did, but I know that what you are
    saying about "\n" not actually getting replaced is wrong...
    Testing this is very tedious. Each time I have to go through and undo
    file modifications that this function performs in addition to the
    above.
    So it is not just a case of making a change and reloading the file
    and rerunning it.
    COPY the file "before" the mods, and just copy it back after.

    Or change your code to just output what it's doing and not overwrite
    the file for development.

    Or...

    --
    Some people have a "gift" link here.
    Know what I want?
    I want you to buy a CD from some indie artist.
    http://cdbaby.com/browse/from/lynch
    Yeah, I get a buck. So?
  • Related Discussions

    Discussion Navigation
    viewthread | post
    Discussion Overview
    groupphp-general @
    categoriesphp
    postedJun 30, '07 at 5:02p
    activeJul 3, '07 at 5:38a
    posts5
    users4
    websitephp.net

    People

    Translate

    site design / logo © 2022 Grokbase