phil_s Posted February 13, 2017 Share Posted February 13, 2017 Hi foks (Not sure if this would make more sense in the dev forum, feel free to move if necessary...) I am trying to do the following... In $page->introtext (a textfield with markdown parser) 1. find the link in which the href matches my $current_urlsegment var 2. Change the href to $link_to_overview 3. Add a ".active" CSS class to it 4. Get the whole thing back into the array 5. Output the array. To add a bit of context: - This is for a text that contains links to segments of a page. When a user clicks on a link (selects a segment), the surrounding text is faded out, and the selected link turns into a "back to overview" button, so to say. (Sounds even more insane, but could work quite intuitively in practice) - So far, I did all of this with js, using a custom data attribute with the current segment to match against the links, but long story short it would be much easier if no js would be involved. As far as I understand it should be rather simple to do this with regex, but I'm struggling with the logic of it. I started with the following: $text = $page->txt_projects_intro; $currentsegment = '/projects/' + $sanitizer->pageName($input->urlSegment1) + '/'; $reg_ex_anchor = '/<a [^>]*\bhref\s*=\s*"\K[^"]*' + $currentsegment + '[^"]*/'; $reg_ex_onlyanchorpath = '[^>]*\bhref\s*=\s*"\K[^"]*' + $currentsegment + '[^"]*'; if(preg_match($reg_ex_anchor, $text, ...)) { $content .= preg_replace... // brain melts } Which is when I realized that I have now googled myself way out of my coding league So: Is the above going into the right direction? Is there an easier way, maybe even involving PW selectors that I don't know about? Cheers Guys! Link to comment Share on other sites More sharing options...
Robin S Posted February 14, 2017 Share Posted February 14, 2017 I don't follow exactly what you're wanting to do, but some thoughts... 1. Seems like Javascript would be a fine way to do this - JS is going to be involved at some point anyway to do your text fading, "back to overview" link, etc. 2. If you do want to do it in PHP, rather than do the field parsing in your template you could make a simple Textformatter module (apply it after Markdown). 3. Parsing and matching HTML content with PHP is generally easier with a dedicated DOM parser such as Simple HTML DOM than regex. 1 Link to comment Share on other sites More sharing options...
kongondo Posted February 14, 2017 Share Posted February 14, 2017 3b. Or PHP's DOM Parser . 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now