473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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?

Jul 17 '05 #1
5 1945
su******@yahoo.co.uk wrote:
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?


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<count($words);$i++){
// is it a number?
if (is_numeric($words[$i])){
$fahrenheit = $words[$i] * blabla;
// (I don't know the conversion)
echo " <span class='temp' title='".$fahrenheit." Fahrenheit'>";
echo $words[$i].$words[$i+1]."</span> ";
$i++;
} else {
echo $words[$i]." ";
}
}
Watch the lines:
echo " <span class='temp' title='".$fahrenheit." 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

Jul 17 '05 #2
Erwin Moller wrote:

$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<count($words);$i++){
// is it a number?
if (is_numeric($words[$i])){
$fahrenheit = $words[$i] * blabla;
// (I don't know the conversion)
echo " <span class='temp' title='".$fahrenheit." Fahrenheit'>";
echo $words[$i].$words[$i+1]."</span> ";
$i++;
} else {
echo $words[$i]." ";
}
}


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($words[$i-1])) {
$celsius = $words[$i-1];
$fahrenheit = $celsius * blabla;
$words[$i-1] = "<span class='temp' title='".$fahrenheit."
Fahrenheit'>".$celsius;
$words[$i] = str_replace("Celsius", "Fahrenheit",
$words[$i])."</span>";
}
}
}
echo implode(" ", $words);

Not tested, but shows the idea.

HTH
Markus
Jul 17 '05 #3
Yes Markus.
Better. :-)

Suzan?
Will this help you enough?

Regards,
Erwin
Jul 17 '05 #4
.oO(su******@yahoo.co.uk)
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?


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

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

$pattern = '#((\d+)\s*celsius)#ei';
$replace = 'sprintf("<span class=\"temp\" title=\"%.1f°F\">$1</span>", C2F($2))';
$text = preg_replace($pattern, $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
Jul 17 '05 #5

Erwin Moller wrote:
Yes Markus.
Better. :-)

Suzan?
Will this help you enough?


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

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Hp | last post by:
Hi All, Thanks a lot for all your replies. My requirement is as follows: I need to read a text file, eliminate certain special characters(like ! , - = + ), and then convert it to lower case and...
2
by: FrzzMan | last post by:
Hello, Is there any function that automatically escape all regex operator in a string? So that the string that contain regex operator will be identified as a *string*, not a *regex pattern*. ...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
4
by: Derek Martin | last post by:
I have an object with several string elements that I would like to check for invalid characters in the properties of each element. Can I use string.replace to do that or is there a better...
16
by: BBM | last post by:
This is so bizarre I hesitate to post it, but I need to get this working. My code looks like this... Private Const cmdTestResourcesSel = "SELECT * FROM TResources" & _ " WHERE Scenario =...
4
by: Lauren Wilson | last post by:
Hi folks, We have a need to replace sub strings in certain message text. We use the Office Assistant to display help and often use the imbedded formatting commands. Those of you who have used...
16
by: ^MisterJingo^ | last post by:
Hi all, I have a variable called root which contains (E:\Web\Websites\fileBrowse\) and then I have a variable called path which uses root, and adds directories on to it (this is part of a file...
5
by: int main(void) | last post by:
Hi all, Following is my attempt to write a string search and replace function. #include <stdio.h> #include <stdlib.h> #include <string.h>...
4
by: sandvet03 | last post by:
I am trying to expand on a earlier program for counting subs and now i am trying to replace substrings within a given string. For example if the main string was "The cat in the hat" i am trying to...
3
by: kronus | last post by:
I'm receiving an xml file that has a child called modified and it represents a date value in the form of a string -- Nov 14, 2008 -- and in my app, I have items associated with each object and I'm...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.