PHP - Adding A Link To Each Word In A String
I have been becoming more and more familiar with PHP over the past few months, but this problem stumps me.
How can I take a string of words separated by spaces taken from an XML feed (example: word1 word2 word3 word4) and turn it into something like this: <a href="word1">word1</a> <a href="word2">word2</a> ... and so on. This has to be done on the fly, since it is taking the words from the XML feed, they change depending on the page. I want them to be in a vertical column, and be able to link each one to the search page for that word, so each link has to be different. An even more in-depth problem, how can I sort it then so that there are multiple columns if necessary, say one page only has 3 words, but another has 50. How can I split them up into multiple vertical columns, say with 10 words in each? Have I confused anyone yet? Similar TutorialsI want to catch the first word of a string in which the words are separated by "-" (e.g. "first-word-second-one-more"). I used this code: $string = explode('-', $string); echo $string[0]; The problem is that when the string contains only one word, it returns null There are many ways to delete everything after a character in a string. I am looking for a simple way to delete everything after a given word. Folks, I want to find a Word in a string. If the word is found, i want to retrun TRUE else FALSE. $string: Sol 3 Drawer Chests Antique Pine Bedside Chest $word to be found: Chest Now the tricky part is: In the $string the $word occurs twice once its CHESTS (i mean the first occurance) then CHEST. If its found, i want to return TRUE else FALSE. I tried using strpos() but it just checks for CHEST and not CHESTS. How can it be achieved? Cheers Natasha Whats the quickest way to find a word in a string and wrap <b> tags around it? Code: [Select] $search = "php web"; $striing = "PHP is the web scripting language of choice"; Basically im looking for the output to be : "<b>PHP</b> is the <b>web</b> scripting language of choice" Hi, and thanks for any help with this.
I have this but of php i have been trying relentlessly to get working.. for over a month. <?php $abs="one two three four words"; $mT = "testing for matching words"; $words=explode(' ', $abs); //$sf=($words); if (preg_match($words, $mT)) { echo "the url $mT contains a Word"; } else { echo "the url $mT does Not contain a Word"; echo "$words[4]"; } ?>
You see, I get a responce only using the last line "$words[4]", (obviously, because I am pointing it the Matching word, (ie: with [4]) Edited June 9, 2020 by x1705 better description. How could I split a string in half but not in the middle of a word? $information = "This is the string of text, however at some point in the sentence a word may get split in half."; $cut = round(strlen($information)/2); ///where to cut (50%) $split1 = substr($information, 0, $cut); //first half $split2 = substr($information, $cut); // second half I have seen somewhere that you can add to a string like so $var = "text"; or $string ="var"; &$var = "text"; it is the latter that I am wanting to know more about. Hi, I currently have a string with some values in it: $a_random_string="some random text, goes here - $123<br>some more random text goes here - $53 (text 123 )<br>ANother line of text - $126"; Now I want to add all the values that have a '$' before the value eg; $123. So in this case I would end up with: 302 How would I do this? Thanks, mme Is it possible to add a query string for example some_var=jk84 to any sort of link be it, http://www.website.com , http://www.website.com/?some_id=4, or http://www.website.com/post=45&category=9 or http://www.website.com/somepost/ ? While adding that extra query string how can I make sure I'm not affecting the website's content or causing some script error? I have a php line like: echo "<p><b>Tags: </b>" . $row['tags'] . "</p>";
it looks like this on web page: how do i add hyperlink to each keyword like: Tags: <a href='category/keyword1'>keyword1</a>, <a href='category/keyword2'>keyword2</a>, <a href='category/keyword3'>keyword3</a>,...
I have a file containing lines (I mean elements separated by <br>). How I can put these lines into an array, but only those which has a given phrase. Example file Code: [Select] This is the first line<br> Second line is here<br> something else<br> something else<br> something more<br> I want to catch only lines which contain the word "something" and make an array. Hi I have an array of items that I am currently displaying a foreach loop What I'm looking to do, is add a string, to all of items, a part from the last 4: foreach ($pager->getResults() as $items => $item) { echo $item->getName(); //need to add a string to this for the all but the last 4 } Thanks Hello everyone, I have this very short script I wrote with help from a book using arrays and the rand function. Basically it changes the images randomly on browser load or refresh. However, I am trying to add an additional, not sure what to call it, a string or another caption for each images. Instead of this being a caption, it will be a link, so a link will appear under each caption that goes to another page when it is clicked on. I tried adding another caption and adding a like to it but getting syntax error. Thanks everyone! Here is the code: Code: [Select] <?php $images = array( array('file' => 'image1', 'caption' => 'Caption 1'), array('file' => 'image2', 'caption' => 'Caption 2'), ); $i = rand(0, count($images)-1); $selectedImage = "graphics/{$images[$i]['file']}.png"; $caption = $images[$i]['caption']; ?> And here is where the images are written to the page: Code: [Select] <div id="stage"> <img src="<?php echo $selectedImage; ?>" alt="Random image" /> <p id="caption"><?php echo $caption; ?></p> </div> IC Hey Guys, I'm querying a database that is going to return a html page. In this page I'm going to define content areas with a class. eg: <div class="category"></div> Anywhere I have this class in the html code, I want to dynamically insert a link into it. So when I retrieve the code from the database, I need the code to check for this tag (class="category") and add a standard link (<a href="..">Add new Category</a>) Not 100% sure whats the most efficient way to go about this. i have a database field called photo. it is used to generate photo id in a software progam for our soccer league. I also want to be able to view the photo on our approval page. could someone help me with the coding of the http h-link? i am having issues with it. the photo field has the following paramaters: images/picture.jpg i want the link on the approval page to look like this: <a href="http://bccsl.org/managers/images/picture.jpg">"http://bccsl.org/managers/images/picture.jpg</a> thanks stefan Code: [Select] <?php /* connection and protection */ include 'dbc.php'; page_protect(); mysql_query( "SET NAMES utf8" ); if(isset($_POST["update"]) AND isset($_POST["hiddenid"])) { $updated=false; $activateapproved=array(); $deactivateapproved=array(); foreach($_POST["hiddenid"] AS $value) { if(isset($_POST["checkboxapproved"][$value])) $activateapproved[]=intval($value); else $deactivateapproved[]=intval($value); } if(count($activateapproved)>0) { $SQL=sprintf("UPDATE players SET approved=1 WHERE id in (%s)" , implode(",", $activateapproved)); mysql_query($SQL) OR DIE(mysql_error()); $updated=true; } if(count($deactivateapproved)>0) { $SQL=sprintf("UPDATE players SET approved=0 WHERE id in (%s)" , implode(",", $deactivateapproved)); mysql_query($SQL) OR DIE(mysql_error()); $updated=true; } if($updated==true) { header("Location: ".$_SERVER["PHP_SELF"].""); exit(); } } ?> <!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=utf-8" /> <title>Player Approval</title> <script type="text/javascript" src="public_smo_scripts.js"></script> <link href="styles.css" rel="stylesheet" type="text/css"> </head> <body> <table width="100%" border="0" cellspacing="0" cellpadding="5" class="main"> <tr> <td colspan="3"> </td> </tr> <tr> <td width="160" valign="top"> <? include 'menu.php'; ?> <p> </p> <p> </p> <p> </p> <p> </p></td> <td width="732" valign="top"><p> </p> <?php if(isset($_GET["todo"]) AND $_GET["todo"]=="updated") { echo "Updated succesfully"; } ?> <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" name="Form"> <table border="1" cellpadding="2" cellspacing="2"> <tr> <td><div align="center">First</div></td> <td><div align="center">Last</div></td> <td><div align="center">Address</div></td> <td><div align="center">City</div></td> <td><div align="center">Postal</div></td> <td><div align="center">Phone #</div></td> <td><div align="center">Feet</div></td> <td><div align="center">Inches</div></td> <td><div align="center">Weight</div></td> <td><div align="center">Birthdate</div></td> <td><div align="center">Team ID</div></td> <td><div align="center">Type</div></td> <td><div align="center">photo</div></td> <td><div align="center">Date Added</div></td> <td><div align="center">Approved</div></td> </tr> <?php $sql="select * from players where teamid='$_POST[teamid]' order by status ASC"; $res=mysql_query($sql) or die(mysql_error()); while($r=mysql_fetch_assoc($res)) { ?> <tr> <input type="hidden" name="hiddenid[]" value="<?php echo $r["id"]?>"> <td> <div align="center"><?php echo empty($r["first"])?' ':htmlspecialchars($r["first"]); ?> </div></td> <td> <div align="center"><?php echo empty($r["last"])?' ':htmlspecialchars($r["last"]); ?> </div></td> <td> <div align="center"><?php echo empty($r["address"])?' ':htmlspecialchars($r["address"]); ?> </div></td> <td> <div align="center"><?php echo empty($r["city"])?' ':htmlspecialchars($r["city"]); ?> </div></td> <td> <div align="center"><?php echo empty($r["postal"])?' ':htmlspecialchars($r["postal"]); ?> </div></td> <td> <div align="center"><?php echo empty($r["phone"])?' ':htmlspecialchars($r["phone"]); ?> </div></td> <td> <div align="center"><?php echo empty($r["feet"])?' ':htmlspecialchars($r["feet"]); ?> </div></td> <td> <div align="center"><?php echo empty($r["inches"])?' ':htmlspecialchars($r["inches"]); ?> </div></td> <td> <div align="center"><?php echo empty($r["weight"])?' ':htmlspecialchars($r["weight"]); ?> </div></td> <td> <div align="center"><?php echo empty($r["birth"])?' ':htmlspecialchars($r["birth"]); ?> </div></td> <td> <div align="center"><?php echo empty($r["teamid"])?' ':htmlspecialchars($r["teamid"]); ?> </div></td> <td> <div align="center"><?php echo empty($r["status"])?' ':htmlspecialchars($r["status"]); ?> </div></td> <td> <div align="center"><?php echo empty($r["photo"])?'<a href="http://bccsl.org/managers/:htmlspecialchars($r["photo"])</a>'; ?> </div></td> <td> <div align="center"><?php echo empty($r["dateadded"])?' ':htmlspecialchars($r["dateadded"]); ?> </div></td> <td> <div align="center"> <input type="checkbox" name="checkboxapproved[<?php echo $r["id"]?>]" value="1"<?php echo empty($r["approved"])?'':' checked="checked"'; ?> /> </div></td> </tr> <?php } ?> </table> <p> <input type="checkbox" name="checkall" onclick="checkUncheckAll(this);"/> select/unselect <input type="submit" value="update" name="update"> </p> </form> <table width="178" border="1" cellpadding="0" cellspacing="1"> <tr> <th colspan="2" scope="col">Type</th> </tr> <tr> <td width="113"><div align="center">Church Player</div></td> <td width="50"><div align="center">1</div></td> </tr> <tr> <td><div align="center">Import Player</div></td> <td><div align="center">2</div></td> </tr> <tr> <td><div align="center">Witness Player</div></td> <td><div align="center">3</div></td> </tr> </table> </td> <td width="196" valign="top"> </td> </tr> <tr> <td colspan="3"> </td> </tr> </table> </body> </html> <a href="http://bccsl.org/managers/">http://bccsl.org/managers/</a> Hey guys, Been working on a project but only recently started using php. What happens is.... I have a general quiz split accross a multipage form and then a results section. The problem is, is that when it comes to if statements on the last page and deciding the difficulty level for the users quiz, it wont let me add the 3rd and final link. Here is the code to explain. I have put bits in bold and a comment. (at the bottom section) I've tried everything, Thanks for any help in advance guys. Code: [Select] <?php session_start(); $_SESSION['q11'] = $_POST['q11']; $_SESSION['q12'] = $_POST['q12']; $_SESSION['q13'] = $_POST['q13']; $_SESSION['q14'] = $_POST['q14']; $_SESSION['q15'] = $_POST['q15']; $protectscore = 0; //initialize score to zero $detectscore = 0; $resolvescore = 0; $overallScore = 0; ?> <html> <head> <title>Results</title> <h1>Protection Section</h1> <p><b>Question 1</b> <?php if ($_SESSION['q1'] == "1"){ echo("<b>Correct</b>"); $protectscore = $protectscore +1; }else{ echo("<b>Incorrect</b>"); } ?><BR> <b>Question 2</b> <?php if ($_SESSION['q2'] == "2") { echo("<b>Correct</b>"); $protectscore = $protectscore +1; }else{ echo("<b>Incorrect</b>"); } ?><BR> <b>Question 3</b> <?php if ($_SESSION['q3'] == "3") { echo("<b>Correct</b>"); $protectscore = $protectscore +1; }else{ echo("<b>Incorrect</b>"); } ?><BR> <b>Question 4</b> <?php if ($_SESSION['q4'] == "1") { echo("<b>Correct</b>"); $protectscore = $protectscore +1; }else{ echo("<b>Incorrect</b>"); } ?><BR> <b>Question 5</b> <?php if ($_SESSION['q5'] == "2") { echo("<b>Correct</b>"); $protectscore = $protectscore +1; }else{ echo("<b>Incorrect</b>"); } ?> <h1>Detection Section</h1> <b>Question 1</b> <?php if ($_SESSION['q6'] == "3") { echo("<b>Correct</b>"); $detectscore = $detectscore +1; }else{ echo("<b>Incorrect</b>"); } ?><BR> <b>Question 2</b> <?php if ($_SESSION['q7'] == "3") { echo("<b>Correct</b>"); $detectscore = $detectscore +1; }else{ echo("<b>Incorrect</b>"); } ?><BR> <b>Question 3</b> <?php if ($_SESSION['q8'] == "1") { echo("<b>Correct</b>"); $detectscore = $detectscore +1; }else{ echo("<b>Incorrect</b>"); } ?><BR><b>Question 4</b> <?php if ($_SESSION['q9'] == "2") { echo("<b>Correct</b>"); $detectscore = $detectscore +1; }else{ echo("<b>Incorrect</b>"); } ?><BR><b>Question 5</b> <?php if ($_SESSION['q10'] == "1") { echo("<b>Correct</b>"); $detectscore = $detectscore +1; }else{ echo("<b>Incorrect</b>"); } ?> <h1>Resolve Section</h1> <b>Question 1</b> <?php if ($_SESSION['q11'] == "2") { echo("<b>Correct</b>"); $resolvescore = $resolvescore +1; }else{ echo("<b>Incorrect</b>"); } ?><BR> <b>Question 2</b> <?php if ($_SESSION['q12'] == "1") { echo("<b>Correct</b>"); $resolvescore = $resolvescore +1; }else{ echo("<b>Incorrect</b>"); } ?><b><BR>Question 3</b> <?php if ($_SESSION['q13'] == "3") { echo("<b>Correct</b>"); $resolvescore = $resolvescore +1; }else{ echo("<b>Incorrect</b>"); } ?><BR><b>Question 4</b> <?php if ($_SESSION['q14'] == "3") { echo("<b>Correct</b>"); $resolvescore = $resolvescore +1; }else{ echo("<b>Incorrect</b>"); } ?><BR> <b>Question 5</b> <?php if ($_SESSION['q15'] == "1") { echo("<b>Correct</b>"); $resolvescore = $resolvescore +1; }else{ echo("<b>Incorrect</b>"); } ?> </p> <hr> <b>Total Sco </b>You answered <?php echo ($protectscore + $detectscore + $resolvescore); ?> question<?php if ($overallScore != 1) { echo("s"); } ?> Correctly <?php $qs = '15'; $overallScore = $protectscore + $detectscore + $resolvescore; $overallPercent = number_format(($overallScore * 100) / $qs); $ProtectionPercent = number_format(($protectscore * 100) / 5); $DetectionPercent = number_format(($detectscore * 100) / 5); $ResolvingPercent = number_format(($resolvescore * 100) / 5); ?><BR> <strong>Total Percentage is:</strong> <?php echo $overallPercent?> % <BR> Your Percentage From Protection Section is: <?php echo $ProtectionPercent?> % <BR> Your Percentage From Detection Section is: <?php echo $DetectionPercent?> %<BR> Your Percentage From Resolving Section is: <?php echo $ResolvingPercent?> %<BR> <HR> This section adds the link perfectly fine <?php if ($ProtectionPercent <= 30){ // beg echo '<li><a href="http://www.google.com> View Personalised Protection Content</a>'; } elseif ($ProtectionPercent > 30 && $ProtectionPercent <= 60){ //int echo '<li><a href="http://www.youtube.com"> View Personalised Protection Content</a>'; } else //exp { echo '<li><a href="http://www.facebook.com">View Personalised Protection Content</a>'; } ?><BR> <HR> This link add fine, however when i add the resolving if statement. This link disappear here and the Resolving section just doesnt appear <?php if ($DetectionPercent <= 30){ // beg echo '<li><a href="http://www.google.com> View Personalised Detection Content</a>'; } elseif ($DetectionPercent > 30 && $DetectionPercent <= 60){ //int echo '<li><a href="http://www.youtube.com"> View Personalised Detection Content</a>'; } else //exp { echo '<li><a href="http://www.facebook.com">View Personalised Detection Content</a>'; } When this part is added, this is when the error occurs <?php if ($ResolvingPercent <= 30){ // beg echo '<li><a href="http://www.google.com> View Personalised Resolving Content</a>'; } elseif ($ResolvingPercent > 30 && $ResolvingPercent <= 60){ //int echo '<li><a href="http://www.youtube.com"> View Personalised Resolving Content</a>'; } else //exp { echo '<li><a href="http://www.facebook.com">View Personalised Resolving Content</a>'; } ?> ?> MOD EDIT: [code] . . . [/code] tags added. Is it possible to add more then one thing in a link? so i can $_GET more then one thing from a link signup.php?state=1?name=sam i tried that and a few other things and it didnt work. I'm trying to add a link to edit my results. Not sure the best way to create the link, nothing I've tried works. There's probably a better way than what I've tried: echo 'There were ', mysql_num_rows($result), ' Matching Photographers:'; while ($row = mysql_fetch_array($result)) { echo "<br />Photographer Info:<br />"; echo "Photographer Name: $row[name]<br />"; echo "Photographer Email: $row[email]<br />"; echo "Photographer ID: $row[PHOTOGRAPHERID]<br />"; echo "Photographer Company: $row[c_name]<br />"; echo "Edit Photographer <a href="update.php?id=' . $row[PHOTOGRAPHERID] . '">Here</a>"; } I am sending emails using PHPMailer. My configuration is below.
When adding a link to the message, should I use anchor tags? Both seem to work. What is the implication to recipients who are not using HTML email?
On a side note, if anything below seems wrong, please let me know.
Thanks
$msg='<p>Hello</p><p>Link: http://phpmailer.worxware.com/</p><p>Link: http://phpmailer.worxware.com</p><a href="http://phpmailer.worxware.com">http://phpmailer.worxware.com</a>'; $mail = new myPHPMailer(true); $mail->AddReplyTo('myEmail@xxx.com', 'Michael Reed'); $mail->SetFrom('myEmail@xxx.com', 'Michael Reed'); $mail->AddAddress('johndoe@xxx.com', 'John Doe'); $mail->Subject = "Here is your email"; //AltBody property need not be sent since PHPMailer will create an alternate automatically $mail->MsgHTML($message); return ($mail->Send()); class myPHPMailer extends PHPMailer { public function __construct($allow_exceptions=false){ $this->isSMTP(); $this->SMTPDebug = 0; $this->Host = "smtp.gmail.com"; $this->Port = 587; $this->SMTPSecure='tls'; // sets the prefix to the server. Used for gmail only. $this->SMTPAuth = true; $this->Username = 'xxx'; $this->Password = 'xxx'; } } |