Jump to content

match very specific letter/nummber combo inside of text


cindreta

Recommended Posts

Hi guys, i am very new to Regex so i would need some guidance here.
 
I am trying to save myself of going trough each article and replacing a bunch of similar tags with something else - to be exact i need to replace all markers marked (S1) with let's say <a href="#" class="modal" data-type="tip" data-number="1"><span class="tip">S</tip></a>. The problem of course is matching what i need.

Example would be:

<p><Lorem Ipsum <strong>(S1)</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. <strong>(O2)</strong> It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like <strong>(S3)</strong> Aldus PageMaker including versions of Lorem Ipsum./p>

As you can see in the example, i will get some HTML content that has keywords like S1...Sx or O1...Ox and i need to find them and replace them with some other HTML content.

 

The pattern can go something like this:

  • starts with letter "S" or "O" or "N" - the letters can be upper or lowecase
  • after the 3 possible letters above there should be a number from 0-9
  • the combination is mostly emebeded inside a HTML strong tag, but it doesn't have to be. If it is in the strong tag then i need to remove that strong tag completly.
  • i need to know the number after the letter so i can use it later to do a database request

I am pretty sure that the patterns S1,O1,N1 will only occur in the cases i need to replace them. I won't have meaningfull text that will have the same pattern.

 

So if someone can help me target this so i can replace it with some anchor tags.

 

Thank you so much.

Edited by cindreta
Link to comment
Share on other sites

UPDATE 1:

 

I have somehow managed to get the following to work:

preg_replace("/(\<strong\>)?\(([son])([0-9])\)(\<\/strong\>)?/i", '<a href="ajax/load-content.php?type=$2&marker=$3&category='.$category_id.'" data-target="#read-modal" data-toggle="modal"><span class="rule-annotation">$2</span></a>', $string);

But this is not completly what i need. For instance it doesnt recognize above the number 9, so if somwhere there would be S12 or N34 it wouldn't replace it. Plus i readlly don't know if i did the strong tag part right, sometimes it might be there and somethimes not.

 

Anyone?

Link to comment
Share on other sites

Stick a + after the number range, like [0-9]+. It means "one or more of the previous".

 

Also,

- You can get rid of some of the backslashes

- If you use # as the delimiter then you don't have to escape the /s

- Throw in some \bs (word boundaries) to avoid some false positives like "ilikeeatingcakes99"

#()?\b\(([son])([0-9]+)\)\b()?#i
- If you're worried about things like Would remote the closing tag S99 you can use a conditional subpattern

#()?\b\(([son])([0-9]+)\)\b(?(1))#i
Link to comment
Share on other sites

Hey requinix,

thank you for your answer. I tried to include your verion which i really find more better looking and shorte than mine but it didn't work, it didnt mark anything.

 

I once again reverted to mine just to check if all is good and for now mine works:

$string = preg_replace("/(\<strong\>)?\(([son])(\d+)\)(\<\/strong\>)?/i", '<a href="ajax/load-content.php?type=$2&marker=$3&category='.$category_id.'" data-target="#read-modal" data-toggle="modal"><span class="rule-annotation">$2</span></a>', $string);

I am very interested in getting your thing to work though, so if you could check why it wouldn't be.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.