Wrap one line paragraphs in string with h tags


 
Thread Tools Search this Thread
Top Forums Web Development Wrap one line paragraphs in string with h tags
# 1  
Old 07-25-2011
Wrap one line paragraphs in string with h tags

Hi,
I have text (string) like this:

Blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah.

Blah blah blah blah

Blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah.


So, we have 3 paragraphs, made by returns and newlines (could be html <p> tags, doesnt matter, we can convert it anyway). Now, every single-line paragraph we want to wrap with <h2> and </h2> tags - make a heading from it.

How could this be done? I have no idea.

Thank you for help.
# 2  
Old 10-04-2011
First question. What scripting language do you plan to use to achieve this? Maybe a server side script such as PHP?
# 3  
Old 10-04-2011
Hi, yeah, right, PHP.
# 4  
Old 10-05-2011
well assuming the text you want to transform is stored in a string variable. Something like this.

PHP Code:
$text "Blah blah blah blah blah blah blah blah blah blah blah blah blah blah  blah blah blah blah blah blah blah blah blah blah blah blah blah blah  blah blah.

Blah blah blah blah

Blah blah blah blah blah blah blah blah blah blah blah blah blah blah  blah blah blah blah blah blah blah blah blah blah blah blah blah blah  blah blah blah blah."
;

$lines preg_split('/\r\n|\r|\n'$text);
$text2 "";
foreach(
$lines as $line)
{
      if(
$line!="")
            
$text2 .= "<h2>".$line."</h2>\r\n";
}

// The text will then be stored in $text2 with the h2 tags in place
//do what you want with it say:

echo $text2
Hope this is what you meant. Unless you meant you want to split up where there is a <p>?
# 5  
Old 10-05-2011
Ah, OK, there may be a more complicated situation, e.g. - there is no empty line between paragraphs. There are several paragraphs and several headlines etc. I have a code now, where it splits all paragraphs and when a paragraph is shorter then given number of letters, it makes a h2 from it...
# 6  
Old 10-05-2011
Not sure i understand exactly what you mean? Have you got some examples to show me?
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk print in one line after reading textfile with paragraphs

Hello everybody I have a text file which has the following format: nmm "text20140601.033954text" "text" "text" "text" , ... , "text" "text" , ... , Lat 36.3247 Lon 16.0588 Depth 8 "text", ... , "text" "text", ..., CovXX 1.65 CovYY 2.32 CovZZ 1.2 "text" , ..., "text nmm ... (6 Replies)
Discussion started by: phaethon
6 Replies

2. Red Hat

How to add a new string at the end of line by searching a string on the same line?

Hi, I have a file which is an extract of jil codes of all autosys jobs in our server. Sample jil code: ************************** permission:gx,wx date_conditions:yes days_of_week:all start_times:"05:00" condition: notrunning(appDev#box#ProductLoad)... (1 Reply)
Discussion started by: raghavendra
1 Replies

3. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

5. Shell Programming and Scripting

Need help with sorting in paragraphs

I am very new to shell scripting, current try to do a sorting of a text file in paragraphs with ksh script. example: File content: A1100001 line 1 = "testing" line 2 = something, line 3 = 100 D1200003 line 1 = "testing" line 2 = something, line 3 = 100 B1200003 line 1 =... (3 Replies)
Discussion started by: gavin_L
3 Replies

6. UNIX for Dummies Questions & Answers

line wrap in sed

I tried line wrap in sed by sed -l 20 ' n' and as far as i understand each long line would be split into lenght of 20 characters in each. but thats not happening.wats going wrong:confused: (5 Replies)
Discussion started by: mithun1!
5 Replies

7. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

8. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies
Login or Register to Ask a Question