Jump to content
php.lv forumi

Izgūt daļu teksta no koda


Rich Bitch
 Share

Recommended Posts

Būtu nepieciešams izgūt teksta daļas no koda (telefona numurus), kas ar javascript tiek sadalīti.

Kods:

...
<script type="text/javascript">//<![CDATA[ 
var kods = "371"; var numurs = "76543210"; document.write(kods + ' ' + numurs)
//]]></script>
...

 

Varbūt kāds var palīdzēt ar ieteikumiem, kā šo telefona numuru ar valsts kodu varētu izgūt no koda un salikt kaut vai masīvā, lai tālāk tos apvienotu?

 

Es iedomājos, ka to varētu ar preg_match, bet es nemāku pareizu "expression" uztaisīt.

Edited by Rich Bitch
Link to comment
Share on other sites

Neticēju un pat notestēju:

<?
header('Content-Type: text/plain; charset=utf-8');

$str = '
...
<script type="text/javascript">//<![CDATA[ 
var kods = "371"; var numurs = "76543210"; document.write(kods + \' \' + numurs)
//]]></script>
...
';

var_dump($str);

$matches = array();
var_dump(preg_match('/var\skods\s=\s"(\d+)";\svar\snumurs\s=\s"(\d+)";/', $str, $matches));
var_dump($matches);
?>

Output:

string(148) "
...
<script type="text/javascript">//<![CDATA[ 
var kods = "371"; var numurs = "76543210"; document.write(kods + ' ' + numurs)
//]]></script>
...
"
int(1)
array(3) {
 [0]=>
 string(42) "var kods = "371"; var numurs = "76543210";"
 [1]=>
 string(3) "371"
 [2]=>
 string(8) "76543210"
}

Link to comment
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
 Share

×
×
  • Create New...