Replace any text from html text by tag name in php

function replace_tag_text($tagName,$subjectStr,$replaceStr) {
$tagName = preg_quote($tagName);
preg_match_all(‘{<‘.$tagName.'[^>]*>(.*?)</’.$tagName.’>}’,$subjectStr,$matches,PREG_PATTERN_ORDER);
$result = str_replace($matches[1],$replaceStr,$subjectStr);
return $result;
}

Usage :
Say for Example

$ActualStr : ‘<div><span style=”margin: 0px 0px 0px 0px; width: 540px;”>something</span><p style=”float: right; margin: -240px 0 0; width: 200px;”>This is the actual String</p></div>’;
$StrToReplaceWith = ‘This is the replaces str’;

Now you want to replace all the text inside the <div> tags from $ActualStr with $StrToReplaceWith
Then you can do as below:

$replacedStr  = replace_tag_text(‘div’,$ActualStr,$StrToReplaceWith);
Result : <div>This is the replaces str</div>

Enjoy 🙂

Create template specific to node in drupal 6

If you want to have custom templates for your node. You can create templates specific to node ID as follows.

Add the below function in your template.php if its not exists.

function yourcurrentthemename_preprocess_node(&$vars, $hook) {
$node = $vars[‘node’];
$vars[‘template_file’] = ‘node-‘. $node->nid;
}

If the function already exists then add the two lines at the beginning of the function.

Then clear the cache.

So if you want to have a custom template for node id 3 (say for example) then create a template with name node-3.tpl.php in your theme directory.

Now you can customise your template as you need.

Keep Rocking…..Enjoy 🙂

How to fix flash object z-index

If any of your html element is getting hide behind the flash object such as youtube video player, or any flash player/object

Then you should call this function on body load.

function fix_flash_z_index() {
// loop through every embed tag on the site
var embeds = document.getElementsByTagName(’embed’);
for (i = 0; i < embeds.length; i++) { embed = embeds[i]; var new_embed; // everything but Firefox & Konqueror if (embed.outerHTML) { var html = embed.outerHTML; // replace an existing wmode parameter if (html.match(/wmode\s*=\s*('|")[a-zA-Z]+('|")/i)) new_embed = html.replace(/wmode\s*=\s*('|")window('|")/i, "wmode='transparent'"); // add a new wmode parameter else new_embed = html.replace(//i))
new_object = html.replace(//i, ““);
// add a new wmode parameter
else
new_object = html.replace(/<\/object\>/i, “\n“);
// loop through each of the param tags
var children = object.childNodes;
for (j = 0; j < children.length; j++) { try { if (children[j] != null) { var theName = children[j].getAttribute('name'); if (theName != null && theName.match(/flashvars/i)) { new_object = new_object.replace(//i, ““);
}
}
}
catch (err) {
}
}
// replace the old embed object with the fixed versiony
object.insertAdjacentHTML(‘beforeBegin’, new_object);
object.parentNode.removeChild(object);
}
}
}

Example to call this function
$(document).ready(function () {
fix_flash_z_index();
});

How to get the allowed option list in drupal theme template or view file

If you want to get the option list of the content type that you have created , then you can get the list as follows:

$content_field = content_fields(‘whatever_the_field_name_is’);
$allowed_values = content_allowed_values($content_field);

So the $allowed_values will give you the array of the option list with the key value association.

Note: The “whatever_the_field_name_is” is the field name that you had specified while adding the field, you can get the field name from your content type .

How to add select option in drupal while creating content type

Whenever you create any content type in drupal ( version 6 and above) and you required a filed of type select list, then you can provide the allowed values of the select option in two ways

CASE 1 :

option 1
option 2
option 3

CASE 2 :

1|option 1
2|option 2
3|option 3

Each new option has to be entered on a new line
So enternally the select list generated would be as follows
In case 1 :

<select>
<option value=”option 1″>option 1</option>
<option value=”option 2″>option 2</option>
<option value=”option 3″>option 3</option>
</select>

In case 2 :

<select>
<option value=”1″>option 1</option>
<option value=”2″>option 2</option>
<option value=”3″>option 3</option>
</select>

Replace multiple space with single space in PHP

If you want to replace multiple space with single space from a string you can use preg_replace function for this. Refer the below example

preg_replace(“!\s+!”,” “,$yourstring);

For example
$yourstring = “This contain space”;
$modifiedString = preg_replace(“!\s+!”,” “,$yourstring);
echo $modifiedString;
// This contain space // output