Karaethon Posted February 25, 2019 Share Posted February 25, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/308394-finding-occurrence-of-values-in-a-string/ Share on other sites More sharing options...
gw1500se Posted February 25, 2019 Share Posted February 25, 2019 I think you are looking for strpos. Quote Link to comment https://forums.phpfreaks.com/topic/308394-finding-occurrence-of-values-in-a-string/#findComment-1564819 Share on other sites More sharing options...
Barand Posted February 25, 2019 Share Posted February 25, 2019 I suggest strtr(). EG $trans = [ '105.f' => "<a href='?search=105.f'>105.f</a>", '106.g' => "<a href='?search=106.g'>106.g</a>", '107.f' => "<a href='?search=107.f'>107.f</a>" ]; $text = "lorem ipsum 105.f dolor sit 106.g amet"; echo strtr($text, $trans); 1 Quote Link to comment https://forums.phpfreaks.com/topic/308394-finding-occurrence-of-values-in-a-string/#findComment-1564827 Share on other sites More sharing options...
Psycho Posted February 26, 2019 Share Posted February 26, 2019 (edited) 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'. Do they always start with a series of digits? If so, what is the minimum/maximum number of digits? Are the digits always followed by a period? 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 Edited February 26, 2019 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/308394-finding-occurrence-of-values-in-a-string/#findComment-1564835 Share on other sites More sharing options...
Karaethon Posted February 26, 2019 Author Share Posted February 26, 2019 Do they always start with a series of digits? If so, what is the minimum/maximum number of digits? Are the digits always followed by a period? 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 Quote Link to comment https://forums.phpfreaks.com/topic/308394-finding-occurrence-of-values-in-a-string/#findComment-1564848 Share on other sites More sharing options...
Psycho Posted February 27, 2019 Share Posted February 27, 2019 (edited) //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); } Edited February 27, 2019 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/308394-finding-occurrence-of-values-in-a-string/#findComment-1564871 Share on other sites More sharing options...
Karaethon Posted February 28, 2019 Author Share Posted February 28, 2019 (edited) 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). Edited February 28, 2019 by Karaethon Quote Link to comment https://forums.phpfreaks.com/topic/308394-finding-occurrence-of-values-in-a-string/#findComment-1564906 Share on other sites More sharing options...
Karaethon Posted February 28, 2019 Author Share Posted February 28, 2019 Ok, so I've been reading about regex and I think this is the fix... am I wayyyyyyy wrong? (\d{3})\.(\d?)(\w?) Quote Link to comment https://forums.phpfreaks.com/topic/308394-finding-occurrence-of-values-in-a-string/#findComment-1564908 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.