Home > OS >  PHP: Reading text in a string within two characters
PHP: Reading text in a string within two characters

Time:10-10

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]},")"))]

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.

CodePudding user response:

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 ')'.

  • Related