473,387 Members | 1,585 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,387 software developers and data experts.

How to change text inside an HTML tag?

I've got a (large, hairy, evil) database full of raw dumps from MySQL
that look like this:

<code>
+----+----------+
| k | t |
+----+----------+
| 1 | apple |
| 2 | orange |
| 3 | pear |
+----+----------+
3 rows in set (0.00 sec)
</code>

When displayed in HTML, the extra spaces don't show, leaving this:

+----+----------+
| k | t |
+----+----------+
| 1 | apple |
| 2 | orange |
| 3 | pear |
+----+----------+
3 rows in set (0.00 sec)

Using <pre> tags won't work, due to other complications with the way
carriage returns are being automatically converted to <br> tags at the
end of each line.

Can anyone recommend a PHP preg_replace statement that would change all
spaces within <code></code> tags to &nbsp; nonbreaking spaces? (This
seems like it ought to be out there somewhere in dozens of examples; if
it is, please feel free to rub my nose in my failure to RTFM.)

I really want to do this with a regular expression and a bunch of other
crawling around; each data record is already filtered through
preg_replace to pop external links up in new browser windows and so
forth. It would be outstanding if I could just add another find pattern
and another replace pattern to the existing module and be done.

Thanks very much,

--Kent
Jul 17 '05 #1
3 2452
"Kent Brewster" <ke**@mindsack.com> wrote in message
news:5K*****************@newssvr27.news.prodigy.co m...
I've got a (large, hairy, evil) database full of raw dumps from MySQL
that look like this:

<code>
+----+----------+
| k | t |
+----+----------+
| 1 | apple |
| 2 | orange |
| 3 | pear |
+----+----------+
3 rows in set (0.00 sec)
</code>

When displayed in HTML, the extra spaces don't show, leaving this:

+----+----------+
| k | t |
+----+----------+
| 1 | apple |
| 2 | orange |
| 3 | pear |
+----+----------+
3 rows in set (0.00 sec)

Using <pre> tags won't work, due to other complications with the way
carriage returns are being automatically converted to <br> tags at the
end of each line.

Can anyone recommend a PHP preg_replace statement that would change all
spaces within <code></code> tags to &nbsp; nonbreaking spaces? (This
seems like it ought to be out there somewhere in dozens of examples; if
it is, please feel free to rub my nose in my failure to RTFM.)

I really want to do this with a regular expression and a bunch of other
crawling around; each data record is already filtered through
preg_replace to pop external links up in new browser windows and so
forth. It would be outstanding if I could just add another find pattern
and another replace pattern to the existing module and be done.

Thanks very much,

--Kent


Kent,
Presumably there would be fewer replacements - thus fewer CPU cycles -
involved in either preventing the conversion of newline to <br> for those
fragments, or post-processing the fragment to revert <br> to newline?
Just my $0.02.
Doug

--
Remove the blots from my address to reply
Jul 17 '05 #2
On Tue, 01 Jun 2004 20:33:05 GMT, Kent Brewster <ke**@mindsack.com>
stated wide-eyed, with arms akimbo:
I've got a (large, hairy, evil) database full of raw dumps from MySQL
that look like this:

<code>
+----+----------+
| k | t |
+----+----------+
| 1 | apple |
| 2 | orange |
| 3 | pear |
+----+----------+
3 rows in set (0.00 sec)
</code>

When displayed in HTML, the extra spaces don't show, leaving this:

+----+----------+
| k | t |
+----+----------+
| 1 | apple |
| 2 | orange |
| 3 | pear |
+----+----------+
3 rows in set (0.00 sec)

Using <pre> tags won't work, due to other complications with the way
carriage returns are being automatically converted to <br> tags at the
end of each line.


Does your database contain discrete data for each column, or are
these text dumps within a database field you're extracting?

If text dumps, g'luck.

If it's discrete (k contains values 1, 2, 3; t contains values apple,
orange, pear, etc.), why not use a table and CSS text classes to make
set-pixel-width boxes, then drop the data into them with a "while"
structure?

Can you post a sample data set?
--
STOP LIVING LIKE VEAL
-----------------------
http://diversify.com Veal-free Websites

Jul 17 '05 #3
On Tue, 01 Jun 2004 20:33:05 GMT, Kent Brewster <ke**@mindsack.com> wrote:
I've got a (large, hairy, evil) database full of raw dumps from MySQL
that look like this:

<code>
+----+----------+
| k | t |
+----+----------+
| 1 | apple |
| 2 | orange |
| 3 | pear |
+----+----------+
3 rows in set (0.00 sec)
</code>

When displayed in HTML, the extra spaces don't show, leaving this:

+----+----------+
| k | t |
+----+----------+
| 1 | apple |
| 2 | orange |
| 3 | pear |
+----+----------+
3 rows in set (0.00 sec)
OK, that's expected.
Using <pre> tags won't work, due to other complications with the way
carriage returns are being automatically converted to <br> tags at the
end of each line.

Can anyone recommend a PHP preg_replace statement that would change all
spaces within <code></code> tags to &nbsp; nonbreaking spaces? (This
seems like it ought to be out there somewhere in dozens of examples; if
it is, please feel free to rub my nose in my failure to RTFM.)

I really want to do this with a regular expression and a bunch of other
crawling around; each data record is already filtered through
preg_replace to pop external links up in new browser windows and so
forth. It would be outstanding if I could just add another find pattern
and another replace pattern to the existing module and be done.


If you've already got newlines sussed, then basically all you want to do is
change any sequence of n spaces (n>1) into a space and (n-1) &nbsp; entities
(if you want to allow longish lines to wrap. If not, then ditch the leading
space and just output n &nbsp;s). Something like (untested):

preg_replace_callback('/( {2,})/',
create_function('$m',
'return " ".str_repeat("&nbsp;",strlen($matches[1])-1);'));

If you've got tabs in there, you'll probably want a prior step turning them
into 4 or 8 spaces depending on your preference. Then present the lot in a
monospace font.

Since you only want it between <code> tags, you probably want that function
called as a callback from another preg_replace_callback('/<code>(.*?)<\/code>',
....). At which point you're probably better of ditching the create_function's
and just making them ordinary functions (I tend to overuse create_function as I
think in Perl for this sort of thing, where anonymous subs have much cleaner
syntax).

So something vaguely like:

function munch_spaces($matches)
{
return ' '.str_repeat('&nbsp;', strlen($matches[1]) - 1);
}

function crunch_code($matches)
{
return preg_replace_callback(
'/( {2,})/',
'munch_spaces',
$matches[1]
);
}

preg_replace_callback(
'/<code>(.*?)<\/code>/',
'crunch_code',
$large_hairy_evil
);

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #4

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

Similar topics

1
by: OM | last post by:
I've found out that I can use <div id = "variable"><div> to change text and pictures on mouseovers. All I have to do is change "variable" to be the HTML text I want. I can even change the text...
6
by: liglin | last post by:
The following script changes the color of text with the onmousover event. How can it be modified so it changes the text when the button is clicked? I'd want to avoid layers or CSS. Thanks,...
5
by: AFN | last post by:
I'm trying to set a submit button to change text and color when clicked. Like saying "please wait" instead of "submit" and then changing the background color and text color. All works, except for...
6
by: TomB | last post by:
Hello all, On the page located at http://deimos.curious.be/~dusk/lyrics.php#gotn I have put the lyrics text inside <pre> elements so that the UA renders the linebreaks in the code. Because...
0
by: Rey | last post by:
Howdy all. Am using visual web developer 2005 (vb), xp pro sp2. In testing of the system.net.mail to send email from an aspx page where I'm pulling the email contents from a textbox, find that...
2
by: amitp | last post by:
hi i'm using MS Word2003 for my VB application which is a report. The word documents contains tables and the cells inside the table contain some fields. My application replaces the field values with...
3
by: SMH | last post by:
Normally an SVG document is loaded/parsed/interpreted inside an HTML document using an 'object' (or 'embed') element, although there are supposedly other ways too. The problem is, the SVG document...
1
by: countocram | last post by:
I have big problem, I'm using preg_replace() function for my highlighter function, after searching for particular keyword, once the hightler check box is checked it will highlight the content that...
18
by: wizdom | last post by:
Help - change text on click - text has another onclick inside with php variables ---------- I think what I'm trying to do is simple. I have a 2 buttons on a page. 1 button allows a thread of...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.