Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP: Reading text in a string within two characters

Hi so I’m still very new to PHP and am using a plugin on wordpress (to import CSV or excel sheets) that has an inline PHP function. I am trying to read an SKU number inside of an entire product description. The SKU number thankfully is within open and closed brackets "(" & ")". I am trying using substr and strpos but the problem I have with my inline code is that I just don’t know how to set the closed bracket as end of the position. It just reads whatever is after the closed bracket because the logic behind strpos is (string,find,start).

So what adjustments am I clearly missing in this shortcode that I can use to adjust it to get it to work the way I want to.

[substr({productdescription[1]},strpos({productdescription[1]},"("),strpos({productdescription[1]},")"))]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is how it would look using actual text which is what I’m using to test first

<?php
echo substr("please read the number (1212) text",strpos("please read the number (1212) text","("),strpos("please read the number (1212) text",")"));
?> 

The plugin at least has a functions section I can write/use but I’d like to know if I can limit it to just using the inline PHP code instead.

>Solution :

Reading a substring between two characters can be done like this:

$a='abc(123)cde'; 
print substr($a,strpos($a,'(')+1,strpos($a,')')-strpos($a,'(')-1);

This should find 123 which is between ‘(‘ and ‘)’.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading