-2

I am trying to modify a php file (It is of Joomla extension Community Builder 1.9.1, and the file is \components\com_comprofiler\plugin\templates\default\default.php), in order to extract the e-mail address from a variable.

For description’s sake, let’s say this variable is $html. To make sure this variable is the right one containing the e-mail address that I'm targeting, I insert:

<pre><?php print_r($html) ?></pre>

Into the file, and its output is the email address with a mailto link, and the corresponding HTML is something like

<span id="cbMa47822" class="cbMailRepl"><a href="mailto:myemail@yahoo.com">myemail@yahoo.com</a></span>

So I guess I can use:

<?php $html_array = explode("\"",$html);echo $html_array[5]; ?>

Io get 'mailto:myemail@yahoo.com'; But actually it only returns a notice of:

undefined offset:5

So I print_r($html_array), and it return something like

Array
(
    [0] =>  cbMa14768
    [2] =>  class=
    [3] => cbMailRepl
    [4] => >... 
)

It looks like the <a> tag part of the html output is replaced by "...", like what you see in Chrome’s developer tool html inspector, where before you expand it, the HTML looks like:

<span id="cbMa47822" class="cbMailRepl">...</span>

I looked deeper into the php code, trying to find out how this $html is contructed, but it is totally beyond my understanding.

For learning purpose, my questions are:

  1. why there is no [1] in the result of print_r($html_array)

  2. How do I test a variable’s value more exactly, by more exactly I mean totally without html input, like if the value is "<a href="htt://foo.com">foo</a>", if should display the HTML as is, but not a link (when I use print_r, it returns a link)?

  3. And most importantly, based on the information given above, can you give my any hint regarding how I can extract the e-mail address from a variable like this?

Finally, for those who are willing to take a deeper look into this, the variable I am talking about is $this->tableContent[$userIdx][1][6]->value in \components\com_comprofiler\plugin\templates\default\default.php, originally it wasn't in the code but I did some test and confirm it contains the email address. I inserted the following code between line 450 & 451

<?php $html_array = explode("\"",$this->tableContent[$userIdx][1][6]->value);echo $html_array[5]; ?>
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
shenkwen
  • 3,536
  • 5
  • 45
  • 85
  • So I provided and answer, but I assumed you are parsing HTML to get an e-mail address. But looking closer at your post while editing, I am not unclear on what the issue is. Are you now trying to find a value passed into a template? – Giacomo1968 Jun 06 '14 at 21:11
  • Sir, your answer is fine and it does help me learn some new tricks. But concerning my original problem, here is some updates(I find out how to inspect a string value as is using `htmlspecialchars()`): if I `pring_r($html)`, it will output an email address with a mailto link, wrapped by a pair of span tag, as you can see in my main post. But if I `echo htmlspecialchars($html)`, it will only output `...`, the `` tag containing the wanted email address is replaced by `...`. I can not understand how this is happening at all. – shenkwen Jun 06 '14 at 22:21
  • The problem is `\components\com_comprofiler\plugin\templates\default\default.php` is a template. You need to fetch the variable in the PHP that sets that value in the template. – Giacomo1968 Jun 06 '14 at 22:39

2 Answers2

0

To extract an e-mail address from an HTML strcuture as you describe, just use regex and preg_match:

$html = '<span id="cbMa47822" class="cbMailRepl"><a href="mailto:myemail@yahoo.com">myemail@yahoo.com</a></span>';

preg_match("/mailto:(.*)\">/is", $html, $matches);

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

The output would be:

Array
(
    [0] => mailto:myemail@yahoo.com">
    [1] => myemail@yahoo.com
)

So to access that e-mail address, just do this:

echo $matches[1];

The output would be:

myemail@yahoo.com
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • Thank you so much for your enlightening answer. I tested the above code and it runs fine on local, but once I replace the $html variable with the actual one ($this->tableContent[$userIdx][1][6]->value) and insert the code into the file for testing, the output is >Array >( >) So I guess the problem is with how the variable was constructed. After all, if your code works with the actual file, my code using explode() should also have worked. – shenkwen Jun 06 '14 at 21:41
  • So now I think the question of mine is focusing on how to print a string value containing html strctures as is,instead of something already interpreted by th browser. Only then I can get a more accurate sense about what excatly $this->tableContent[$userIdx][1][6]->value contains and establish a way to extract email address from it thereafter. – shenkwen Jun 06 '14 at 21:52
-1
  1. To avoid links you can use escape sequence.
  2. you can use regular expression to match if the given string matches the email address pattern and print it
  3. PHP has a vast support for functions which can perform wierdest tasks so search for them
Gaurav Singh
  • 65
  • 10
  • Thank you sir, could you please be more specific? Like if I have a string with the value of "foo", how can I get it to display the value as is but not something already interpreted by the browser(which will be "foo" with a link only) – shenkwen Jun 06 '14 at 21:05
  • I think a minor change in Jake's regex preg_match("/(.*)/", $html, $matches); it will work as it will return to you complete url foo another thing take care of escape sequence – Gaurav Singh Jun 09 '14 at 19:36