Page 1 of 1

Inserting custom tags into a string

Posted: Fri Nov 13, 2009 7:18 am
by Grizzzzzzzzzz
I have a simple PHP script which parses through HTML files, it removes certain tags and special characters, and replaces with with others using. I can alter about 95% of each file to the format i want without a problem, however i've run into an issue when i want to wrap certain strings in tags, for example when it comes up against something like this


Code: Select all

 
 
....
 
– Example 1:  asasdergergegerg asfdsf.
 
– Example 2: asdfs fergseg erggdfgsr.
 
– Example 3: werwe rsegsergsegafsd fasdf2.
 
– Example 4: gdfgjkhgsergser gajkgahwklf.
 
– Example 5:  gdfggse gserkhajkgahwklf.
 
– Example 6:  gdfgjrgaergerhajkga hwklf.
 
– Example 7:  gdgaw ergwerggjkhajkg ahwklf.
 
– Example 8:  gdf gjkhajk dfgerg ega eggahwklf.
 
....
 
 


I need to wrap each -'d bullet point in a tag, like this;

Code: Select all

– Example 1:  asasdergergegerg asfdsf.
will become

Code: Select all

<SpecialTag, startBullet> Example 1:  asasdergergegerg asfdsf. <SpecialTag, endBullet>

i can start it off easily enough, by using

Code: Select all

 
str_replace("–", "<SpecialTag, startBullet>", $fileData);
 
i was wondering if there is something simliar that'll replace the '-', ignore the following text and the write the end tag after the following '.'


any ideas would be appreicated

Re: Inserting custom tags into a string

Posted: Fri Nov 13, 2009 8:54 am
by Apollo

Code: Select all

$text = "- Example 1:  asasdergergegerg asfdsf.";
 
$text = preg_replace("/^[\x96\\-]([^.]*)\\./","<SpecialTag, startBullet>\\1.<SpecialTag, endBullet>",$text);
 
// hooray, $text is now "<SpecialTag, startBullet> Example 1: asasdergergegerg asfdsf.<SpecialTag, endBullet>"
Welcome to the wonderful world of Regular Expressions ;)

Re: Inserting custom tags into a string

Posted: Fri Nov 13, 2009 11:38 am
by Grizzzzzzzzzz
that would work perfectly if there wasn't 8000 other lines of random crap also with the file


Cheers anyway, i've managed to work out a solution using a recursive function.

Re: Inserting custom tags into a string

Posted: Sat Nov 14, 2009 5:09 am
by Apollo
If you have the entire contents of your file in one string, you could have also done it by simply removing the first ^ at the beginning of the regular expression (the one after the starting slash). Or add an 'm' modifier after the closing slash (for multiline).

So:

Code: Select all

// This will also replace subsequent stuff between dashes and dots (does not have to be at the beginning of a line)
$text = preg_replace("/[\x96\\-]([^.]*)\\./","<SpecialTag, startBullet>\\1.<SpecialTag, endBullet>",$text);
Or:

Code: Select all

// This will only replace stuff between dashes and dots at the beginning of each line, but $text may contain multiple lines and they're all processed
$text = preg_replace("/^[\x96\\-]([^.]*)\\./m","<SpecialTag, startBullet>\\1.<SpecialTag, endBullet>",$text);