ereg_replace replaces to mutch

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default ereg_replace replaces to mutch

    Hello,

    I use a javascript ticker in my header which contains the news in a array
    like that
    tickercontents[0]='Message1'
    tickercontents[1]='Message2'
    tickercontents[2]='Message3'
    now i want to write an easy script that i can run from my server to change
    the message and
    was thinking about something like that

    $replacement = 'New Message1';
    $file = "header.html";
    $fh = fopen($file,"r") or die("can't open file");
    $file = fread($fh, filesize($file)) or die("can't read file");
    $replaced =
    ereg_replace("(tickercontents\[0\]\=)(')(.*)(')","\\1\\2$ersatz\\4",$file1)
    or die("can't do that sorry");
    ..........
    the only problem is ereg_replace in not stopping at the end high coma in
    message1 it stops at the end high coma of message3.
    I there a way to make it stop after the first one.
    I appreciate any thoughts
    Thanks
    chris


    Chris Guest

  2. Similar Questions and Discussions

    1. Contribute replaces ? with %3F when creating a link...
      Hello, This problem occurs sometimes and we couldn't find out why it happenes. When creating a link we chose the destination php file and add...
    2. extension that searches for certain characters and replaces them
      Hey, I am trying to develop a new extension for dwmx which searches for certain characters and replaces them with other characters! How do I go...
    3. ereg_replace
      > $linked_comments = ereg_replace("\n","<br>",$linked_comments"); Try: $linked_comments = str_replace("\r\n","\n",$linked_comments");...
    4. Can xml replaces a server technology
      If that is the case, can I use xml in conjunction with asp? Or, is there a way to make vbscript compatible to browser other than IE. I ask this...
  3. #2

    Default Re: ereg_replace replaces to mutch

    Chris wrote:
    > ereg_replace("(tickercontents\[0\]\=)(')(.*)(')","\\1\\2$ersatz\\4",$file1)
    > the only problem is ereg_replace in not stopping at the end high coma in
    > message1 it stops at the end high coma of message3.
    > I there a way to make it stop after the first one.
    > I appreciate any thoughts
    Try:
    preg_replace("/tickercontents\[0\]='([^']*)'/iU","tickercontents[0]='$ersatz'",$file1);

    --
    Justin Koivisto - [email]spam@koivi.com[/email]
    PHP POSTERS: Please use comp.lang.php for PHP related questions,
    alt.php* groups are not recommended.

    Justin Koivisto Guest

  4. #3

    Default Re: ereg_replace replaces to mutch

    Rather than using replace

    "Chris" <cmohn@gmx.de> wrote in message
    news:vqfsc84rd4u975@corp.supernews.com...
    > Hello,
    >
    > I use a javascript ticker in my header which contains the news in a array
    > like that
    > tickercontents[0]='Message1'
    > tickercontents[1]='Message2'
    > tickercontents[2]='Message3'
    > now i want to write an easy script that i can run from my server to change
    > the message and
    > was thinking about something like that
    >
    > $replacement = 'New Message1';
    > $file = "header.html";
    > $fh = fopen($file,"r") or die("can't open file");
    > $file = fread($fh, filesize($file)) or die("can't read file");
    > $replaced =
    >
    ereg_replace("(tickercontents\[0\]\=)(')(.*)(')","\\1\\2$ersatz\\4",$file1)
    > or die("can't do that sorry");
    > .........
    > the only problem is ereg_replace in not stopping at the end high coma in
    > message1 it stops at the end high coma of message3.
    > I there a way to make it stop after the first one.
    > I appreciate any thoughts
    > Thanks
    > chris
    >
    >

    A slight variation of your approach would be to use PHP as the Preprocessor
    that it was designed as. This may not fit your requirements, but you then
    again it may give you something to ponder:

    In "header.php"

    <html>
    ....
    <script>
    ....
    <?PHP
    // Assume you ahve a list of message in the array $messageList
    $index = 0;
    foreach ($messageList as $message)
    print "tickercontents[".$index++."]='".$message."'";
    ?>
    ...
    </script>
    .....

    Good luck!
    Christopher


    ChronoFish Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139