Help with adding html code to plain text

I had planned on doing extensive editing and thought it would be simple but I am failing at the first hurdle.
Say I have a plain text file that I have read in and it is sitting in a variable…
I want to wrap html code round specific strings that I know are in there somewhere…
If I have those strings as an array, shouldn’t I be able to do a for loop and a str_replace, for example…
[php]$known = array(’…’,’…’,… );
$txt = count($known);
for ($i=0; $i<$txt; $i++)
$string = str_replace($known[i],’

’.$known[i].’

’,$string);[/php]
or a preg_replace? Is there any escaping required?

The issue with that is, you would be overwriting the variable each time. It is better to have an array of replacement values and an array of replacement placeholders. Then, you replace everything at once.

Thanks.
Overwriting the variable each time? Isn’t that what replacing is intended to do?
It isn’t clear to me what you are suggesting but something like this?
[php]$known = array(’…’,’…’,… );
$tobe = array(’

’,’

’,… );
$string = str_replace($known,$tobe,$string);[/php]

I’ve already tried it and it isn’t working.
Is there any escaping required?

Is it a length problem? These strings are 40-50 characters long. If I reduce them to 4 or 5 characters, it works fine.

OK, it is working fine now. It seems overkill as I have had to hard code duplicate long variables, as opposed to dynamically creating them by adding the h tags front and back. I’m sure there must be a better way.

God, if I have this much trouble creating a table from what looks like a table, I may as well give up.

I apologize in advance for the word count on here, but it is to illustrate a point.

[php]$replace = <<<REP
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla.

Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh.

Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. Suspendisse in justo eu magna luctus suscipit.

Sed lectus. Integer euismod lacus luctus magna. Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Morbi lacinia molestie dui. Praesent blandit dolor. Sed non quam. In vel mi sit amet augue congue elementum. Morbi in ipsum sit amet pede facilisis laoreet. Donec lacus nunc, viverra nec, blandit vel, egestas et, augue. Vestibulum tincidunt malesuada tellus. Ut ultrices ultrices enim. Curabitur sit amet mauris.

Morbi in dui quis est pulvinar ullamcorper. Nulla facilisi. Integer lacinia sollicitudin massa. Cras metus. Sed aliquet risus a tortor. Integer id quam. Morbi mi. Quisque nisl felis, venenatis tristique, dignissim in, ultrices sit amet, augue. Proin sodales libero eget ante. Nulla quam. Aenean laoreet. Vestibulum nisi lectus, commodo ac, facilisis ac, ultricies eu, pede.

REP;

echo $replace;

$str = explode("\n\r", $replace);
foreach ( $str as $string ){
echo “

$string

”;
}
[/php]

OK, the text comes in from a txt file with about 50 lines of text that is visually “formatted,” including some tables, and only has lf’s at end of line (invariably mid-sentence). I just wanted to clean it up a bit. What I was trying to do, initially, was make three known embedded sentences, that appear in the text, as headings.
This is the code that I ended up with (there is additional code to add in other tags) to do that…
[php]
$textfrom = array(‘A. NOAA Geomagnetic Activity Observation and Forecast’,
‘B. NOAA Solar Radiation Activity Observation and Forecast’,
‘C. NOAA Radio Blackout Activity and Forecast’);
$textto = array(‘

A. NOAA Geomagnetic Activity Observation and Forecast

’,

B. NOAA Solar Radiation Activity Observation and Forecast

’,

C. NOAA Radio Blackout Activity and Forecast

’);
$string = str_replace($textfrom,$textto,$string);[/php]

…which I think is overkill. I couldn’t get any alternative (that I tried), where the replacement string was generated from the original, to work.

It doesn’t matter now because, as well as adding the tags, I am changing the original text in those headings so this approach is probably ideal.

Anyway, now I have to figure out how I can make what looks like a table into an actual table, as opposed to using

.
Sponsor our Newsletter | Privacy Policy | Terms of Service