String replace help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • susan0nz@yahoo.co.uk

    String replace help

    I have some strings like this: "The temperature is 18 Celsius today".
    Some strings may have more than one value in them, like "The
    temperature is 18 Celsius today. Yesterday is was 17 Celsius".

    I want to display these, adding a tooltip when the mouse pointer is
    over the "18C section, such that it shows the value in Fahrenheit
    instead of Celsius. Something like this:

    The temperature is <span class="temp" title="64 Fahrenheit">18
    Celsius</span> today.

    I need some way to do a string replace to add the <span>, but with a
    calculation to work out the new value. Can preg_replace do something
    like this?

  • Erwin Moller

    #2
    Re: String replace help

    susan0nz@yahoo. co.uk wrote:
    [color=blue]
    > I have some strings like this: "The temperature is 18 Celsius today".
    > Some strings may have more than one value in them, like "The
    > temperature is 18 Celsius today. Yesterday is was 17 Celsius".
    >
    > I want to display these, adding a tooltip when the mouse pointer is
    > over the "18C section, such that it shows the value in Fahrenheit
    > instead of Celsius. Something like this:
    >
    > The temperature is <span class="temp" title="64 Fahrenheit">18
    > Celsius</span> today.
    >
    > I need some way to do a string replace to add the <span>, but with a
    > calculation to work out the new value. Can preg_replace do something
    > like this?[/color]

    Hi

    I am unsure how to manage that with preg_replace. Probably it is possible.
    But you can easily code it yourself like this:

    $temp = "The temperature is 18 Celsius today. Yesterday is was 17 Celsius";
    $words = explode(" ",$temp);

    // you now have a array with all the words.
    // loop through all:
    for($i=0;$i<cou nt($words);$i++ ){
    // is it a number?
    if (is_numeric($wo rds[$i])){
    $fahrenheit = $words[$i] * blabla;
    // (I don't know the conversion)
    echo " <span class='temp' title='".$fahre nheit." Fahrenheit'>";
    echo $words[$i].$words[$i+1]."</span> ";
    $i++;
    } else {
    echo $words[$i]." ";
    }
    }


    Watch the lines:
    echo " <span class='temp' title='".$fahre nheit." Fahrenheit'>";
    echo $words[$i].$words[$i+1]."</span> ";
    $i++;

    In there I also include the next word (Celsius), and also must increase the
    counter $i by 1 extra. If not done, you will see Celsius 2 times.
    But if your sentence end with a number this gives an array out of bound
    error.

    I didn't check this code, but I guess you will manage from here.
    Good luck.

    Regards,
    Erwin Moller

    Comment

    • Markus Ernst

      #3
      Re: String replace help

      Erwin Moller wrote:[color=blue]
      >
      > $temp = "The temperature is 18 Celsius today. Yesterday is was 17
      > Celsius"; $words = explode(" ",$temp);
      >
      > // you now have a array with all the words.
      > // loop through all:
      > for($i=0;$i<cou nt($words);$i++ ){
      > // is it a number?
      > if (is_numeric($wo rds[$i])){
      > $fahrenheit = $words[$i] * blabla;
      > // (I don't know the conversion)
      > echo " <span class='temp' title='".$fahre nheit." Fahrenheit'>";
      > echo $words[$i].$words[$i+1]."</span> ";
      > $i++;
      > } else {
      > echo $words[$i]." ";
      > }
      > }
      >[/color]

      I like your idea. Anyway you get a problem with:

      $temp = "My 11 year old daughter said, the temperature was 18 Celsius, but
      it was actually 17 Celsius."

      As a modification suggestion:

      $words = explode(" ", $temp);
      for ($i=0; $i<count($words ); $i++) {
      // Check for "Celsius"
      // I use strstr because there could be a punctuation or something
      if (strstr($words[$i], "Celsius")) {
      if (is_numeric($wo rds[$i-1])) {
      $celsius = $words[$i-1];
      $fahrenheit = $celsius * blabla;
      $words[$i-1] = "<span class='temp' title='".$fahre nheit."
      Fahrenheit'>".$ celsius;
      $words[$i] = str_replace("Ce lsius", "Fahrenheit ",
      $words[$i])."</span>";
      }
      }
      }
      echo implode(" ", $words);

      Not tested, but shows the idea.

      HTH
      Markus


      Comment

      • Erwin Moller

        #4
        Re: String replace help

        Yes Markus.
        Better. :-)

        Suzan?
        Will this help you enough?

        Regards,
        Erwin

        Comment

        • Michael Fesser

          #5
          Re: String replace help

          .oO(susan0nz@ya hoo.co.uk)
          [color=blue]
          >I have some strings like this: "The temperature is 18 Celsius today".
          >Some strings may have more than one value in them, like "The
          >temperature is 18 Celsius today. Yesterday is was 17 Celsius".
          >
          >I want to display these, adding a tooltip when the mouse pointer is
          >over the "18C section, such that it shows the value in Fahrenheit
          >instead of Celsius. Something like this:
          >
          >The temperature is <span class="temp" title="64 Fahrenheit">18
          >Celsius</span> today.
          >
          >I need some way to do a string replace to add the <span>, but with a
          >calculation to work out the new value. Can preg_replace do something
          >like this?[/color]

          Yep, with the modifier /e which interprets the replacement string as PHP
          code.

          function C2F($temp) {
          return ($temp + 32) * 1.8;
          }

          $pattern = '#((\d+)\s*cels ius)#ei';
          $replace = 'sprintf("<span class=\"temp\" title=\"%.1f°F\ ">$1</span>", C2F($2))';
          $text = preg_replace($p attern, $replace, $text);

          If you want a longer title, use this instead:

          $replace = 'sprintf("<span class=\"temp\" title=\"%.1f Fahrenheit\">$1 </span>", C2F($2))';

          HTH
          Micha

          Comment

          • susan0nz@yahoo.co.uk

            #6
            Re: String replace help


            Erwin Moller wrote:[color=blue]
            > Yes Markus.
            > Better. :-)
            >
            > Suzan?
            > Will this help you enough?[/color]

            I went with the method suggested by Michael Fesser, but thanks to
            everyone for the other suggestions.

            Comment

            Working...