1

I spent all morning researching and, with the help of SO, figured out how to grab the innerHTML of elements using Javascript.

This is to populate meta tags for search and social open graph. The tag in question here is the meta description tag.

What I used in Javascript was this:

document.getElementsByClassName("report-description-text")[0].childNodes[2].nodeValue;

The (somewhat awkward) html structure is this:

<!-- start report description -->
<div class="report-description-text">
<h5>Description</h5>
Welcome to Ushahidi. Please replace this report with a valid incident
<br/>
</div>

I want to grab the text "Welcome to Ushahidi...". But this text is not inside any html tags, it's just text. The above JS works in grabbing that text.

I then tried to integrate this into a PHP script but was swiftly downvoted since, I now know, that you cannot execute JS on the server side (I knew that but never had enough practice to really get it).

The PHP script looks like this:

if( strpos($url, '/reports/view') === 0 ) { 
echo '<!-- Make this site discoverable, shareable and searchable -->
<meta name="description" content=[figure out what to put here]/>
// deal with these other metas later                    
<meta name="author" content="my name" />

I'd like the content of the content to be, in this example: content="Welcome to Ushahidi...." />

How would I do that using PHP? Is it as straightforward as with JS? I did some research but just worked myself into confusion.

Doug Fir
  • 19,971
  • 47
  • 169
  • 299
  • Look into the `DOMDocument` class, which is a DOM parser in PHP. http://www.php.net/manual/en/class.domdocument.php. – Hidde Apr 12 '14 at 15:12
  • php is run before the html.So that text is known to php. so, what are you trying to do again? – Aris Apr 12 '14 at 15:23
  • @Aris I want to output a metatag in the header like so: This is for all the pages of this nature. The description in the div element above should be grabbed and used to populate the meta tags – Doug Fir Apr 12 '14 at 15:31
  • How is that DIV element being generated? If it's something recurring you can use a variable on the HTML page, for example $topDIV; this variable can then be echoed in the metatag aswell. (basically PHP knows of the text before it's being sent to the browser, PHP serves it(using includes normally), so it's all about varaibles. – Patrick Apr 12 '14 at 15:36
  • @Patrick yes! Of course. The best answers are the obvious ones and I never considered this (I don;t have much experience). Next step will be to research which variable is being spat out here. Will research the platforms documentation (https://wiki.ushahidi.com/display/WIKI/Ushahidi+v2.X+Developer+Guide) – Doug Fir Apr 12 '14 at 15:38
  • possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – Quentin Apr 12 '14 at 15:39

1 Answers1

1

You must understand the PHP is run on the server side first, and then the Html is rendered in client side.

You should try to face the problem the other way around. Try something along the following lines:

In PHP define :

$description = "Welcome to Ushahidi. Please replace this report with a valid incident";

You can use this variable in all you PHP code:

echo '<!-- Make this site discoverable, shareable and searchable -->
<meta name="description" content=['.$description.']/>
// deal with these other metas later                    
<meta name="author" content="my name" />

Then on the html, you can still use this variable, since it is already set:

<h5>Description</h5>
<?php echo $description; ?>
<br/>
Aris
  • 4,643
  • 1
  • 41
  • 38
  • Thanks for the answer @Aris I'm going to do some research on the back of Patricks comment. I'm using a platform and there must be a variable already set that outputs the description. The description is stored int he database for each report page (The page in question is a report page). Therefore there must be a preexisting variable that outputs this. There is not magic tool to see what variable is being passed here is there? For example I' a data analyst and use HTTPfox often to see what Google Analytics parameters are being passed. Separate question I guess – Doug Fir Apr 12 '14 at 15:41
  • my answer and Patricks comment say the same thing.No, you cannot see the php parameters, unless you have access to the code. – Aris Apr 12 '14 at 15:43
  • I have access to the code in that it's my website and I have ftp access. Is there a conventional way or do folk typically trial and error with echo? – Doug Fir Apr 12 '14 at 15:45
  • This is a good answer, and as Aris stated, you must have access to the actual code to see what's being parsed. – Patrick Apr 12 '14 at 15:46
  • you just need to find the related file and see how the value is displayed. Even if it's plain html, you can always change it to a PHP variable. – Aris Apr 12 '14 at 15:48