Jump to content

Finding occurrence of values in a string.


Karaethon

Recommended Posts

I have a DB of data: article-id, section-id, subsection-id, article-text. When a row is fetched I am displaying article, section, subsection as <h3>, <h4>, <h5> and then text in a <p>. Before outputting the text I want to scan it for other articles, sections, or subsections it may reference then turn that reference into a hyperlink. I read the PHP documentation but I can't find anything that translates to the basic commands of instring, indexof, and left/mid/right. 

Example:

1. Article Name

100. Article 1, section 1

100.a article 1, section 1, subsection a

Text blah blah blah 105.f blah blah blah

 

I want to find that 105.f and turn it into a link. All my articles, sections, and subsections are in the DB so I know I can use an array of the number values as my items to look for, but I can't figure out how to look.

Link to comment
Share on other sites

I think the best solution is RegEx [specifically preg_replace() or preg_match()], but to provide a solution would require the "specs" for the article references. You gave one example where the reference was three digits + period + the letter 'f'.

  1. Do they always start with a series of digits? If so, what is the minimum/maximum number of digits?
  2. Are the digits always followed by a period?
  3. Is the period always followed by a letter? If so, what are the valid letters and are they always lower case?

Also, how is that reference supposed to be modified to a URL? I.e. what would the URL look like for your example of '105.f'?

 

Here is an example

function insertReferences($text)
{
    return preg_replace("#(\d{3})\.(\w)#is", '<a href="displayArticle.php?id=${1}&type=${2}">${1}.${2}</a>', $text);
}
	//Text from DB
$articleText = 'Beginning text and then a reference 105.f to another article';
//Modify text to include hyperlinks
$outputText = insertReferences($articleText);
//Output the result
echo $outputText

Output:

Beginning text and then reference <a href="displayArticle.php?id=105&type=f">105.f</a> to another article

Link to comment
Share on other sites

  1. Do they always start with a series of digits? If so, what is the minimum/maximum number of digits?
  2. Are the digits always followed by a period?
  3. Is the period always followed by a letter? If so, what are the valid letters and are they always lower case?

Also, how is that reference supposed to be modified to a URL? I.e. what would the URL look like for your example of '105.f'?

 

Ok...

 1. Yes each section is three digits.

2. No, at present they are not but I could add the period if that would be required

3. Yes, if there is a subsection then that subsection is numbered as [section number].[subsection letter]

4(?) Each separate data piece is in a bootstrap collapse with an ID of "sectionNNN.L" or "sectionNNN" as appropriate. So I will be wrapping the reference in

<span class="btn btn-link" data-target="section???" onclick=" showPage('section???')" data-parent=" #page">???</span>

Specifically the data is the comprehensive rules text at magic.wizards.com/rules 

Link to comment
Share on other sites

//I broke out the code into multiple lines for readability and maintainability
function insertReferences($text)
{
    $regEx = "#(\d{3})\.(\w)#is";
    $format ='<span class="btn btn-link" data-target="section${1}" onclick="showPage('section${1}')" data-parent="#page">${1}.${2}</span>';
    return preg_replace($regEx, $format, $text);
}

 

Link to comment
Share on other sites

Oh Shoot. I just noticed I had a small typo, its ###.# or ###.#L. How does that change the regex?

Ok, I used what you wrote and it does (almost) exactly what I want, the only error was (my fault completely) it did not capture the trailing 6 on a reference to 100.6 and it did not capture the trailing i on 100.4i. I bet its an easy fix if I grocked regex. But I don't (yet).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.