some php assistance

Status
Not open for further replies.

filthy123

(>゚∀゚)>
Oct 16, 2007
97
2
0
Is there a way, with php, to insert code in the source code before a specific/defined keyword?

eg. say i have a little javascript that shows information about a certain work, like blue widgets. so when blue widgets is displayed and you float your mouse over it a little pop up will come up saying a few things about blue widgets.

what i'd like to do is make an include file that dynamically inserts that java code whenever blue widgets appears on the screen, without me having to always manually put in the code.

is this possible?
 


i think a good example of what i'm looking to do is like those snap ads you see on some sites, that turns keywords into links...but what i want to do is instead of making the keyword a link, rather put some code before it
 
I'm not 100 percent sure if I get what you are saying, but if I understand right, some AJAX code for the mouseover could dynamically pull the text you want from the server...
 
i want to create an include file that has something like if a word like blue widgets exists on a page, insert this code before it. so i'd include that file onto my page, and when it finds the word blue widgets, it simply adds it before
 
You can do that with PHP by using the output control functions:
Code:
<?php
ob_start();
/* do your thing */
$out = ob_get_contents();
ob_end_clean();
/* do search & replaces on $out to get the result you want */
$echo $out1;
?>
Downside of this method is that the visitor will only see something when the processing reaches that last line.

You can also use JavaScript to go through your document when loading is complete and do search & replaces.

Edit: Woohoo, post #200! :D
 
ok :)

i have an standard html file, with content on it about blue widgets. what i would like to do is have a certain piece of code (say something like javascript) inserted into the html before the words blue widgets, everytime blue widgets is mentioned in the html file (or rendered).

this way, instead of having to change the code in however many places blue widgets is mentioned, i only need to change one file.

so the include file would "scan" the html (or php file if it needs to be) for certain keywords and inject code into the html/php when that keyword appears
 
ok :)

i have an standard html file, with content on it about blue widgets. what i would like to do is have a certain piece of code (say something like javascript) inserted into the html before the words blue widgets, everytime blue widgets is mentioned in the html file (or rendered).

this way, instead of having to change the code in however many places blue widgets is mentioned, i only need to change one file.

so the include file would "scan" the html (or php file if it needs to be) for certain keywords and inject code into the html/php when that keyword appears

Yes, that's what the code in my previous post does ;)
 
cool thanks!

it's actually a mix of both that I need :) I need the tooltip "injected" into the code whenever a certain word is displayed.

cygnusx, could you perhaps give me an example of that script with the information filled in? like i want to to put hello world before the words blue widgets and goodbye world before the words red widgets?
 
If you want to do text replacements via PHP then CygnusX is right; you can easily use the output buffer. Eg:

Code:
<? ob_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
cool thanks!

it's actually a mix of both that I need I need the tooltip "injected" into the code whenever a certain word is displayed.

cygnusx, could you perhaps give me an example of that script with the information filled in? like i want to to put hello world before the words blue widgets and goodbye world before the words red widgets?
</body>
</html>
<?
$content = ob_get_clean();

$find = array("blue widgets",
              "red widgets");
              
$replace = array("<span style='color:red'>hello world</span> blue widgets",
                 "<span style='color:blue'>goodbye world</span> red widgets");              

$content = str_replace($find,$replace,$content);

echo $content;

?>
But because you want a tooltip you'll need to use JavaScript. You can either use PHP to write out the JavaScript code, or just use it to write out a variable. For example:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style1 {color: #00CCCC}
-->
</style>
</head>
<body>
cool thanks!

it's actually a mix of both that I need I need the tooltip "injected" into the code whenever a certain word is displayed.

cygnusx, could you perhaps give me an example of that script with the information filled in? like i want to to put hello world before the words blue widgets and goodbye world before the words red widgets?
<script language="javascript">
    //Replacements
    content = document.body.innerHTML;
    content = content.replace(/(blue widgets)/i,"<span class='mousover' title='Hello World' id='blue'>$1</span>");
    content = content.replace(/(red widgets)/i,"<span class='mousover' title='Goodbye World' id='red'>$1</span>");
    document.body.innerHTML = content;
    
    //Mouseovers
    hoverdivs = [];
    spans = document.getElementsByTagName('span')
    for(i=0;i<spans.length;i++) {
        spans[i].onmouseover = function() {
             hoverspan=document.createElement('span');
             hoverspan.style.color = this.id;            
             hoverspan.innerHTML = this.title;
             this.appendChild(hoverspan);
        }
        spans[i].onmouseout = function() {
            span = this.getElementsByTagName('span');
            this.removeChild(span[0]);
        }        
    }
    
</script>
</body>
</html>
That code will cause a very simple example mouseover effect on red and blue widgets. So you could use PHP to write these lines, taking the info from your database or whatever.
Code:
    content = content.replace(/(blue widgets)/i,"<span class='mousover' title='Hello World' id='blue'>$1</span>");
    content = content.replace(/(red widgets)/i,"<span class='mousover' title='Goodbye World' id='red'>$1</span>");
So it might look something like:
Code:
<? foreach($replacements AS $replacement) { ?>
content = content.replace(/(<?=$replacement['from']?>)/i,"<span class='mousover' title='<?=$replacement['to']?>' id='red'>$1</span>");
<? } ?>
My advice would be to go find a tooltip you like, and get it working on normal HTML first. Then use PHP to add in all the data.
 
  • Like
Reactions: sgtryan
thanks!

this is EXACTLY what i was looking for :) thanks d60eba!
i've got it to work using the first example. i've found a tooltip that i want to use, here is the code:

Code:
<a style="text-decoration:none; color:#FFD700" name="staticLink" href="javascript:void(0);" onmouseover="docTips.show('blue widgets')" onmouseout="docTips.hide()">
<img src="bluewidgets.gif" hspace="2" width="12" height="12" align="absmiddle"></a>

should that code replace the <span style='color:red'>hello world</span> in the $replace array?
 
should that code replace the <span style='color:red'>hello world</span> in the $replace array?

Yes, that will replace all instances of blue widgets with your blue widgets gif. If you want to keep the blue widgets text use

Code:
<a style="text-decoration:none; color:#FFD700" name="staticLink" href="javascript:void(0);" onmouseover="docTips.show('blue widgets')" onmouseout="docTips.hide()">
<img src="bluewidgets.gif" hspace="2" width="12" height="12" align="absmiddle">Blue Widgets</a>
 
Status
Not open for further replies.