473,387 Members | 1,517 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.

Extract text from tags multiple times

170 100+
Hi,

If I have this:

Today is a good day
[LINK]Johnny is good[/LINK]
Tomorrow will be better
[LINK]Mary is bad[/LINK]
Who cares

And there could be anywhere between 0 and 4 occurrences of the [LINK]...some text...[/LINK] tags, how can I extract the strings

"Johnny is good" and "Mary is bad" ?

Really nice would be to have these stored in an array, such as

$myarray=array(1=>'Johnny is good',2=>'Mary is bad');

Any pointers or ideas? Many thanks!
Dec 4 '08 #1
3 1776
Markus
6,050 Expert 4TB
You'll almost certainly need to use a regular expression. Check out preg_match(), this code snipper on BBCode (very much like what you're doing) and also check out the 3rd parameter of preg_match() - it will store matches into an array for you.
Dec 4 '08 #2
beary
170 100+
Hey there Markus,

As usual, you've been a superb help, and I really appreciate it! My actual problem was to take

Today is a good day
[LINK]Johnny is good[/LINK]
Tomorrow will be better
[LINK]Mary is bad[/LINK]
Who cares

and then match each of the strings inside the link tags to a record in mysql, in order to grab the actual path to that file, so I could then create the url instead of the links tags.

So now, after looking at the pages you suggested, I have
Expand|Select|Wrap|Line Numbers
  1. function bbcode_html($input) {
  2. $bbcode = array(
  3. '/\[link=(.+?)\](.+?)\[\/link\]/i'
  4. );
  5.  
  6. $html = array(
  7. '<a href="$1">$2</a>'
  8. );
  9. return preg_replace($bbcode, $html, $input);
So, if I run a query to find my magical pathway, such as
Expand|Select|Wrap|Line Numbers
  1. echo bbcode_html('Today is a good day<br>
  2. [LINK=myfirstpath]Johnny is good[/LINK]<br>
  3. Tomorrow will be better<br>
  4. [LINK=mynextpath]Mary is bad[/LINK]<br>
  5. Who cares');
it will now return the html

Today is a good day
<a href='myfirstpath'>Johnny is good</a>
Tomorrow will be better
<a href='mynextpath'>Mary is bad</a>
Who cares

Which is perfect.

And I can now easily extend to using [web] tags or similar for simple url web links. It needed to be so a user could simply place tags around a web link and have it create the url for them.

I think I better get my head around regular expressions. They seem pretty powerful. Again, thanks for the advice.

Cheers

@Markus
Dec 4 '08 #3
Dormilich
8,658 Expert Mod 8TB
@beary
note: you actually need not stick with / as RegEx delimiter, every char will do as long as it doesn't have a special meaning. so you can skip some of the escapes (well, your case it's only one, but there may be times it comes in handy). e.g.
Expand|Select|Wrap|Line Numbers
  1. '@\[link=(.+?)\](.+?)\[/link\]@i'
Dec 4 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Jasper Bryant-Greene | last post by:
I have three tables: `photos`, `tags` and `tags_photos`. The `photos` table contains a unique ID and a bunch of other stuff, the `tags` table contains a unique ID and a tag name, and the...
10
by: mark4 | last post by:
Hello, Are there any utilities to help me extract Content from HTML ? I'd like to store this data in a database. The HTML consists of about 10,000 files with a total size of about 160 Mb....
4
by: Patrick | last post by:
I've got some text with a few HTML tags, such as the following <Bold>Hello</Bold>There buddy<p>please ..... I need to be able to extract just the text, which would be Hello there buddy...
13
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them...
9
by: gregmcmullinjr | last post by:
Hello, I am new to the concept of XSL and am looking for some assistance. Take the following XML document: <binder> <author>Greg</author> <notes> <time>11:45</time>
1
by: Petra Meier | last post by:
Hi, I hope this is a proper place to post :) This is my XML file: <?xml version="1.0" encoding="utf-8"?> <foo> <address>a lot of tags and text</address> <hubid>a lot of tags and text</hubid>...
1
by: Alberto Sartori | last post by:
Hello, I have a html text with custom tags which looks like html comment, such: "text text text <p>text</ptext test test text text text <p>text</ptext test test <!-- @MyTag@ -->extract...
0
by: peter pilsl | last post by:
For feeding the content of an xml-file to a search-indexer I need to remove all tags and extract the plaintext out of a xml-file. I use the null-xls-stylesheet <?xml version="1.0"?>...
5
by: Steve | last post by:
Hi all Does anybody please know a way to extract an Image from a pdf file and save it as a TIFF? I have used a scanner to scan documents which are then placed on a server, but I need to...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.