Solved

Regular Expression Pattern Needed

Posted on 2003-11-27
8
285 Views
Last Modified: 2009-07-29
Can anyone help me

I have a string

some text here <p> some more text <p><a href=\"a link\"> something</a> <br>else<br> a new line now <br>please<br> and another new <p>paragraph<p>

I want to check my string and if any of the text on a new line is less than 10 chars then make it bold


so it would output something like:

This is some very interesting text here that is longer than ten chars<p>

<strong>this isn't</strong><p>

this is some more long text here<br>

<strong>this is <a href="something.com">short<a/></strong>


any gifted programmers out there?
0
Comment
Question by:stuayre
  • 4
  • 2
8 Comments
 
LVL 1

Expert Comment

by:vincevincevince
Comment Utility
$string=preg_replace("/\n([^\n]{1,10})\n/m","\n<strong>$1</strong>\n",$string);

i.e. find a newline, followed by 1 to 10 characters which aren't a new line, followed by another newline

if this is found, put <strong> </strong> around the found string
0
 
LVL 1

Author Comment

by:stuayre
Comment Utility
thanks vince

but I can't seem to get it to work properly with my text :( here's an example of the text I'm using,
I'd like:
History Main article: History of England
Politics Main article: Politics of England
Subdivisions Main article: Subdivisions of England
Geography Main article: Geography of England
List of towns in England
Economy Main article: Economy of England
Demographics Main article: Demographics of England
Culture Main article: Culture of England

all in the <strong> tag, is it possible?


<p>
(This article is about the nation of England. There is also England, Arkansas in the United States of America.)
<p>

<p>
England is the largest, the most populous, and the most densely populated of the nations that make up the United Kingdom of Great Britain and Northern Ireland.
<p>
The name "England" is derived from "Engla-lond" or "land of the Angles".
It is often incorrectly used as a synonym for Great Britain or the United Kingdom by some, which is inaccurate and can be offensive.
Other terms for England include "Blighty", from the Hindustani "bila yati" meaning "foreign"; "this Green and Pleasant Land", from William Blake's poem Jerusalem. "Albion" was used by writers such as Pliny the Elder and Ptolemy in the 1st century, in reference to the white (Latin: "alba") cliffs of Dover.
<p>
The Marquis de Ximenés, an 18th century diplomat, is credited with coining the phrase La perfide Albion, or "perfidious Albion", which is still heard from the French -- also an affectionate term, in its own way. It is also used by the Irish about the English but in a less affectionate manner, suggesting a degree of untrustworthiness. The Irish also refer to England as "pagan England".
<p>
<table border=1 cellpadding=2 cellspacing=0 align=right width=285px>
England
<tr><td style=background:#efefef; align=center colspan=2>
<table border=0 cellpadding=2 cellspacing=0>
<tr><td align=center width=140px>
<td align=center width=140px>

<tr><td align=center width=140px>(In Detail)
<td align=center width=140px>(Lions of England)
</table>
<tr><td align=center colspan=2 style=border-bottom:3px  ;>Royal motto: Dieu et mon droit (God and my right)
<tr><td>Official languages<td>None, but mostly English is used by public officials.
<tr><td>Capital<td>London
<tr><td>Area<br>&nbsp;- Total<td>Ranked 1st <br>130,395 km&sup2;
<tr><td>Population <br>&nbsp;- Total (2001) <br>&nbsp;- Density <td>Ranked 1st<br>49,138,831<br>377/km&sup2;
<tr><td>Unification<td>Egbert of Wessex (d.839) is often regarded as the first king of all England, though his true title was Bretwalda (High King). School histories of England tend to begin with the accession of William the Conqueror in 1066.
<tr><td>Currency<td>Pound Sterling
<tr><td>Time zone<td>UTC+0
<tr><td>National anthems<td>God Save the Queen, Land of Hope and Glory (unofficial), Jerusalem (unofficial)
</table>
<p>
<p><table border<tr><td align
<b>Table of contents</b> </td></tr><tr id='dfuhduifhdfhtocinside'><td align

1  History <BR>


2  Politics <BR>


3  Subdivisions <BR>


4  Geography <BR>


5  Economy <BR>


6  Demographics <BR>


7  Culture <BR>


8  Miscellaneous Topics <BR>


9  External links <BR>

</td></tr></table><P>
 History
Main article: History of England
<p>
 Politics
Main article: Politics of England
<p>
England, as a significant political entity, ceased to exist with the Act of Union 1707, which created the Kingdom of Great Britain.  All of Great Britain has been ruled by the government of the United Kingdom between that date and 1999, when the first elections to the newly created Scottish Parliament and Welsh Assembly left England as the only nation in the Union with no representative body.
<p>
There are calls by some for an English Parliament to counteract what they see as a democratic imbalance. Many would have this parliament take the form of an all-England grand committee in the United Kingdom House of Commons rather than as a new body with separate elections.
<p>
The current Labour government favours the establishment of regional governments, claiming that England is too large to be governed as a sub-state entity.  It's currently unclear whether there is any significant support for this from the public, especially since people generally identify with counties, rather than regions that have little historical basis.
There is, however, substantial public support in several regions, particularly in the north of England. Referenda will take place on this issue, possibly some time in 2004, and consideration has still to be given to what powers regions would be granted, and what impact this may have on the powers of counties or central government.  Considerable disquiet was caused when changes were made to the system of counties in 1974.  
<p>
Unlike the other nations of the Kingdom, there is very little call for independence of England from the UK. This is overwhelmingly due to its dominance in the Union. Those groups that do campaign for such a thing tend to be right-wing organisations.
<p>
 Subdivisions
Main article: Subdivisions of England
<p>
Historically, the highest level of local government in England was the county.  These divisions had emerged from a range of units of old, pre-unification England, whether they were Kingdoms, such as Essex and Sussex; Duchies, such as Yorkshire, Cornwall and Lancashire or simply tracts of land given to some noble, as is the case with Berkshire.
<p>
These counties all still exist in, or near to their original form as the traditional counties. In many places, however, they have been heavily modified or abolished outright as administrative counties. This came about due to a number of factors.
<p>
The fact that the counties were so small meant, and still means, that there was no regional government able to co-ordinate an overarching plan for the area.  This was especially true in the metropolitan areas surrounding the cities, as the county lines were usually drawn up before the industrial revolution and the mass urbanisation of the country.
<p>
The solution was the creation of large metropolitan counties centred on cities. These were later broken up, with several other counties, into unitary authorities, unifying the county and district/borough levels of government.
<p>
London is a special case, and is the one Region which currently has a representative authority as well as a directly elected mayor. The thirty-two London boroughs remain the local form of government in the city.
<p>
Other than Greater London, the official Regions are:
North East England
North West England
Yorkshire and the Humber
West Midlands
East Midlands
East of England
South West England
South East England
The Regions hold very little power owing to their lack of accountability - regional authority is placed in the hands of unelected representatives of various interests. When, as seems likely, several Regions opt to replace these QUANGOs with elected assemblies, Local government in England will remain as variable and, some might say, confusing as ever.
<p>
 Geography
Main article: Geography of England
<p>
England comprises most of the southern two-thirds of the island of Great Britain. It is bordered to the north by Scotland and to the west by Wales.<br>
Most of England consists of rolling terrain, but the country is more mountainous in the north. The dividing line between terrain types is usually indicated by the Tees-Exe line.<br>
<p>
The Channel Tunnel near Dover links England to the European mainland.
<p>
<p>
Major rivers:
 Thames
 Severn
 Trent
 Humber
 Tyne
 Mersey
 Dee
Major cities:
 London
 Birmingham
 Manchester
 Bristol
 Liverpool
 Leeds
 Newcastle upon Tyne
 Sheffield
<p>
List of towns in England
<p>
 Economy
Main article: Economy of England
<p>
 Demographics
Main article: Demographics of England
<p>
England is both the most populous and the most ethnically diverse country in the United Kingdom with around 49 million inhabitants, of which roughly a tenth are from non-White ethnic groups.<br>
<p>
This population is made up of immigrants who have arrived over millennia. The principal waves of migration have been in c. 600 BC (Celts), the Roman period (garrison soldiers from throughout the Empire), 350-550 (Angles, Saxons, Jutes), 800-900 (Vikings, Danes), 1066 (Normans), 1650-1750 (European refugees and Huguenots), 1880-1940 (Jews), 1950-1985 (Caribbeans, Africans, South Asians), 1985-present (East Europeans, Kurds, refugees).  
<p>
The general prosperity of England has also made it a destination for economic migrants particularly from Ireland and Scotland. This diverse ethnic mix continues to create a diverse and dynamic language that is widely used internationally.
<p>
Generally, an English person is someone who lives in England regardless of their racial origin. However, some people (including many south Asians and whites) use the label as only referring to those people of Anglo-Saxon origin - preferring to instead use "British" as a racially neutral label.  This is only possible due to the somewhat hazy distinction that many people in the country make between "England" and "Britain".
<p>
See also Population of England - historical population estimates
<p>
<p>
 Culture
Main article: Culture of England
<p>
English literature
Shakespeare
Sir Thomas Browne
English national football team, English Football League teams (Soccer)
National parks (England and Wales)
Food and Drink
<p>
 Miscellaneous Topics
English Law
 List of monarchs of England - Kings of England family tree
 List of English people
 Angeln (region in northern Germany)
 UK topics
 List of British postal codes
0
 
LVL 1

Expert Comment

by:vincevincevince
Comment Utility
$string=preg_replace("\n([^\n]*\n[^\n]*Main article: [^\n]*)\n","\n<strong>\1</strong>\n",$string);
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Author Comment

by:stuayre
Comment Utility
thanks Vince,

I should have said that my text will change all the time, I need a generic preg_replace for strings < 10 (or whatever)
0
 
LVL 1

Expert Comment

by:vincevincevince
Comment Utility
$string=preg_replace("\n([^\n]{1,10})\n","\n<strong>\1</strong>\n",$string);

That does it... on my machine ;)
0
 
LVL 1

Accepted Solution

by:
vincevincevince earned 146 total points
Comment Utility
sorry, you'll need:

$string=preg_replace("\n([^\n]{1,10})\n","\n<strong>$1</strong>\n",$string);
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now