Asked  7 Months ago    Answers:  5   Viewed   33 times

I have a JavaScript file where I would like to include some php code. The problem is that I have a few defines on PHP that I would like to use on JS as well.

Is there any way of including a .js file in HTML allowing the server to first interpret it (before downloading to the client) using php?

Thanks :)


<script src="/path/to/my/file.php"></script>

In file.php you'll also want to output the correct header, before outputting anything you should have the following:

header("Content-Type: application/javascript");

EDIT: As @Tony_A pointed out, it should be application/javascript. I don't think it mattered as much when I wrote this post in 2010 :)

Wednesday, March 31, 2021
answered 7 Months ago

No, it doesn't have one. For this reason most popular libraries come with one in their utility packages. Check out jQuery's inArray and Prototype's Array.indexOf for examples.

jQuery's implementation of it is as simple as you might expect:

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    return false;

If you are dealing with a sane amount of array elements the above will do the trick nicely.

EDIT: Whoops. I didn't even notice you wanted to see if an array was inside another. According to the PHP documentation this is the expected behavior of PHP's in_array:

$a = array(array('p', 'h'), array('p', 'r'), 'o');

if (in_array(array('p', 'h'), $a)) {
    echo "'ph' was foundn";

if (in_array(array('f', 'i'), $a)) {
    echo "'fi' was foundn";

if (in_array('o', $a)) {
    echo "'o' was foundn";

// Output:
//  'ph' was found
//  'o' was found

The code posted by Chris and Alex does not follow this behavior. Alex's is the official version of Prototype's indexOf, and Chris's is more like PHP's array_intersect. This does what you want:

function arrayCompare(a1, a2) {
    if (a1.length != a2.length) return false;
    var length = a2.length;
    for (var i = 0; i < length; i++) {
        if (a1[i] !== a2[i]) return false;
    return true;

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(typeof haystack[i] == 'object') {
            if(arrayCompare(haystack[i], needle)) return true;
        } else {
            if(haystack[i] == needle) return true;
    return false;

And this my test of the above on it:

var a = [['p','h'],['p','r'],'o'];
if(inArray(['p','h'], a)) {
    alert('ph was found');
if(inArray(['f','i'], a)) {
    alert('fi was found');
if(inArray('o', a)) {
    alert('o was found');
// Results:
//   alerts 'ph' was found
//   alerts 'o' was found

Note that I intentionally did not extend the Array prototype as it is generally a bad idea to do so.

Wednesday, March 31, 2021
answered 7 Months ago

You have to use the object you have received:

$phpWord = PhpOfficePhpWordIOFactory::load($source, 'MsDoc');

It is a multidimensional object of arrays and objects, and you have to locate [elements] property, in which you have to locate [text] property. This [text] property contains the text extracted from your Word file.

Please bear in mind that by default these two properties are protected, so you will have to change their status in the PHPWord library files - for [elements] it is AbstractContainer.php, and for [text] it is Text.php. Once you have changed the status of these two properties to public, you can extract them from your $phpWord object.

I now can extract text from .doc files, but what I noticed is that PHPWord will just extract some 60% of text from any .doc file, sometimes just cutting the last word it extracted by half. So, if your file has 4,000 words, PHPWord gets only some 2,000 of them, somehow.

I am at a loss here, actually, as to why PHPWord does not want to get ALL the text. No notices, no exceptions, just an object without a good half of text from a .doc file.

Saturday, May 29, 2021
answered 5 Months ago

Here is a quick and dirty standalone hack using regex:

$input = '
#stuff {
    background-color: red;

#content.postclass-subcontent {
    background-color: red;

#content2.postclass-subcontent2 {
    background-color: red;

$cssClassName = 'postclass';
preg_match_all('/(#[a-z0-9]*?.?'.addcslashes($cssClassName, '-').'.*?)s?{/', $input, $matches);

Results in:

array(2) {
  string(29) "#content.postclass-subcontent"
  string(31) "#content2.postclass-subcontent2"
Wednesday, June 9, 2021
answered 5 Months ago

You can switch the version here:

1. Press CTRL+ALT+S

2 Search for JavaScript & click the select field. and then select ECMAScript 6

View image.

Sunday, August 22, 2021
RajaReddy PolamReddy
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :