0

I have the code tag "div" its value will change cause i use jquery on click. i using code ob_get_contents() for passing to php variable, but tag "div" also join. how can i get value only?

Example : inbox.js

jQuery(document).ready(function($) {
var cols = {}, messageIsOpen = false;
cols.showMessage = function() {
    $('body').addClass('show-message');
    messageIsOpen = true;
};
cols.hideMessage = function() {
    $('body').removeClass('show-message');
    $('.message-list li').removeClass('active');
messageIsOpen = false;
};
$('.messagelist .li')on('click', function(e){
  var item = $(this);
  if(messageIsOpen && item.is('.active')) {
    cols.hideMessage();
  }else{
    item.addClass('active');
    cols.showMessage();
    var drnm = $(".active .darinama").text();
    $(".from").text(drnm);
  }
});
});

inbox.php

<div class="messagelist">
  <ul>
    <li>
      <div class="darinama">Ahmad</div>
    </li>
    <li>
      <div class="darinama">Galih</div>
    </li>
    <li>
      <div class="darinama">String name</div>
    </li>
  </ul>
</div>
<div id=message>
  <?php
    php ob_start();
  ?>
  <div class="from"></div>
  <?php 
    $nm = ob_get_contents();
    $strnm = substr($nm, 20);
    ?>
</div>

result $nm is

< div class="drnama">Galih< /div>

want result just Galih

result $strnm is

ma">

substr not working i want to process the data in php

1 Answers1

0

You could use a regular expression to parse the value.

$input_line1 = '<div class="drnama">Galih</div>';
$input_line2 = '<li>list item</li>';

// works for any HTML tag
$regex = "/<([\w]+)[^>]*>(.*?)<\/([\w]+)>/";

$nm = preg_match($regex, $input_line2, $output_array);
print_r($output_array);

/*
Array
(
    [0] => <li>list item</li>
    [1] => li
    [2] => list item
    [3] => li
)
*/

$nm = preg_match($regex, $input_line1, $output_array);
print_r($output_array);

/*
Array
(
    [0] => <div class="drnama">Galih</div>
    [1] => div
    [2] => Galih
    [3] => div
)
*/

Demo

Andy Hoffman
  • 18,436
  • 4
  • 42
  • 61
  • echo $input_line; tag "div" still join echo $nm; result is 1 what the meaning of 1? – Ahmad Galih Dec 07 '17 at 23:43
  • @AhmadGalih I edited the answer. It should work now. – Andy Hoffman Dec 07 '17 at 23:59
  • no result, but if
    i change to
    bcd
    on click at Galih change to
    Galih
    but the result $output_array[1] is bcd
    – Ahmad Galih Dec 08 '17 at 00:09
  • If you visit the [demo link](http://sandbox.onlinephpfunctions.com/code/0162bca8c000fa67a14ac40011513ef5cdf52a6f) and execute the code, you can see it working. If the `$input_line` is equal to `
    bcd
    `, executing the code will produce `bcd` when printing `$output_array[1]`. Is this what you are looking for? Did I misunderstand your question?
    – Andy Hoffman Dec 08 '17 at 00:12
  • almost approaching the tag "div" was gone, but not the bcd I want. i want the value in tag "li" {Ahmad, Galih, or String name} – Ahmad Galih Dec 08 '17 at 00:19
  • @AhmadGalih I think I understand you better now. You need this to work for any HTML tag, not just a `div`. Have a look at the updated answer and [demo link](http://sandbox.onlinephpfunctions.com/code/1ebb6965755ef374077ce9702b2fc449218349eb) – Andy Hoffman Dec 08 '17 at 00:46
  • array [2] => null i think this code action before js function active – Ahmad Galih Dec 08 '17 at 01:17
  • I'm sorry, but I don't understand what you're looking for. – Andy Hoffman Dec 08 '17 at 01:53