/    Sign up×
Community /Pin to ProfileBookmark

PHP cant conver string that is taken from getElementByid PHP , result is Zero

can someone help me to solve the code of my php.
please help me..
PHP can’t read string converted into integer, the result is always 0

here is the code

[code]
<html>
<body>
<div id=’div1′></div>
<div id=’div2′>
<H2>demo string to integer </H2>
<p> the example of value is <span style=”font-style: italic;”>12</span> </p>
<?php $shtm=”<div id=’a’>12</div>” ;?>
</div>
</body>
</html>

<?php
$doc = new DOMDocument();
$doc->loadHTML($shtm);
$div = $doc->getElementById(“a”);

$nval= $div->ownerDocument->saveHTML($div);

echo ‘1. result before converted :’. $nval .'</br>’;
echo ‘2. using gettype : ‘. gettype($nval) .'</br>’;
echo ‘3. convert to integer using (int) :’ . (int)$nval.'</br>’;
echo ‘4. try using intval function :’ .intval($nval) .'</br>’; // it should be 13 but the result is the same
echo ‘5. try adding with 1 : ‘.(int)$nval+1 ;// it should be 13 but the result is error A non well formed numeric value encountered

?>[/code]

**(Added `[code]` tags ~ MOD)**

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 28.2019 — You're getting the whole element, not just the value:
<i>
</i>23:05 $ php -a
Interactive shell

php &gt; $shtm="&lt;div id='a'&gt;12&lt;/div&gt;" ;
php &gt; $doc = new DOMDocument();
php &gt; $doc-&gt;loadHTML($shtm);
php &gt; $div = $doc-&gt;getElementById("a");
php &gt;
php &gt; $nval= $div-&gt;ownerDocument-&gt;saveHTML($div);
php &gt; var_export($nval);
'&lt;div id="a"&gt;12&lt;/div&gt;'

Maybe try textContent?
<i>
</i>php &gt; $nval = $div-&gt;textContent;
php &gt; var_export($nval);
'12'
×

Success!

Help @mmansyur2000 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...