3

I need to extract a specific letter or number from string.

<div id="craftysyntax_1" style="float: right;"><script type="text/javascript" src="https://livehelp.clipboards.com/livehelp_js.php?eo=0&amp;department=1&amp;serversession=1&amp;pingtimes=10&amp;dynamic=Y&amp;creditline=W"></script></div>

From this div with id="craftysyntax_1" I want to extract just the number 1 from this craftysyntax_1, i am trying with explode but it does not work for me or maybe I am doing something wrong.

Here is what I have tried :

$myString = '<div id="craftysyntax_1" style="float: right;"><script type="text/javascript" src="https://livehelp.clipboards.com/livehelp_js.php?eo=0&amp;department=1&amp;serversession=1&amp;pingtimes=10&amp;dynamic=Y&amp;creditline=W"></script></div>';
            $strArray = explode('craftysyntax_', $myString, 1);
            print_r($myString);

How can I achieve what I want?

Haroon
  • 496
  • 5
  • 14
  • 31

5 Answers5

1
preg_match("/craftysyntax_(.*)\\" /", $myString, $output_array);
echo $output_array[1];

Good luck!

podarok
  • 537
  • 5
  • 12
1

You can use a regEx to accomplish your goal as in this example

$myString = '<div id="craftysyntax_123" style="float: right;"><script type="text/javascript" src="https://livehelp.clipboards.com/livehelp_js.php?eo=0&amp;department=1&amp;serversession=1&amp;pingtimes=10&amp;dynamic=Y&amp;creditline=W"></script></div>';
$pttn = '@craftysyntax_(\d{1,})@';
preg_match( $pttn, $myString, $matches );

echo '<pre>',print_r($matches,1),'</pre>';

This will output:

Array
(
    [0] => craftysyntax_123
    [1] => 123
)

so you can explicitly target the integer using $matches[1]

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
1

I want to extract just the number 1 from this craftysyntax_1

Use preg_match function:

$myString = '<div id="craftysyntax_1" style="float: right;"><script type="text/javascript" src="https://livehelp.clipboards.com/livehelp_js.php?eo=0&amp;department=1&amp;serversession=1&amp;pingtimes=10&amp;dynamic=Y&amp;creditline=W"></script></div>';

preg_match("/id=[\"']craftysyntax_(\d+)[\"']/", $myString, $matches);
$craft_number = $matches[1];

print_r($craft_number);  // 1
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
1

Try this by using preg_match:

$myString = '<div id="craftysyntax_1" style="float: right;"><script type="text/javascript" src="https://livehelp.clipboards.com/livehelp_js.php?eo=0&amp;department=1&amp;serversession=1&amp;pingtimes=10&amp;dynamic=Y&amp;creditline=W"></script></div>';
preg_match("/id=[\"']craftysyntax_(\d+)[\"']/", $myString, $output);
print_r($output);  //Array ( [0] => id="craftysyntax_1" [1] => 1 )
print_r($output[1]);//1
Nimish
  • 1,006
  • 1
  • 7
  • 19
0

You really shouldn't use regex to try and parse HTML.

An alternative solution, use DOMDocument to extract the ID you want:

$str = '<div id="craftysyntax_1" style="float: right;"><script type="text/javascript" src="https://livehelp.clipboards.com/livehelp_js.php?eo=0&amp;department=1&amp;serversession=1&amp;pingtimes=10&amp;dynamic=Y&amp;creditline=W"></script></div>';

$dom = new DOMDocument;
$dom->loadHTML($str);
$src = $dom->getElementsByTagName('div')->item(0);
$attr = $src->attributes->getNamedItem('id')->value;

This will give you the value craftysyntax_1. Now you can easily grab everything after the last underscore, if it exists:

if (($pos = strrpos($attr, '_')) !== false) {
    echo substr($attr, $pos + 1);
}
Community
  • 1
  • 1
mister martin
  • 6,197
  • 4
  • 30
  • 63