php str_replace function issue - php

I am trying to have this ORIGINAL string converted to the RESULT below using php.
ORIGINAL: "The quick <font color="brown">brown</font> fox jumps over the lazy dog"
RESULT:"god yzal eht revo spmuj xof <font color="brown">nworb</font> kciuq ehT"
What I have done so far is explained like below.
First, strip the HTML tag from the ORIGINAL.
$originalStr = "The quick <font color='brown'>brown</font> fox jumps over the lazy dog";
$stripTags = strip_tags($originalStr);
This results to The quick brown fox jumps over the lazy dog ,
Second, I reverse the result and the word "brown" by using strrev function
$reverseStr = strrev($stripTags);
$brown = strrev("brown");
This results to god yzal eht revo spmuj xof nworb kciuq ehT
Third, I am trying to use str_replace function to find $brown from the reverseStr, and replace it with $openFont $brown $closeFont like below.
$openFont = "<font color='brown'>";
$closeFont = "</font>";
$result = str_replace($brown, $openFont.$brown.$closeFont, $reverseStr);
echo "result -->" . $result . "<br/><br/><br/>";
This results to god yzal eht revo spmuj xof kciuq ehT, NOT the same as the RESULT.
It seems like special characters in font () tag is the problem that may be blocking str_replace to replace the String.
$result = str_replace($brown, "TEST", $reverseStr);
echo "result -->" . $result . "<br/><br/><br/>";
This results to god yzal eht revo spmuj xof TEST kciuq ehT
Does anyone know str_replace is not accepting special characters? and know how I should solve this problem?
If there is another way to solve the problem, I will also be appreciated to hear your suggestion.
(* This is one of the practical questions that I am trying to solve in an algorithm test websites)
UPDATED: I feel so dumb to think that where font tag was. Since tag is meant to change the font color, it was working perfectly in the beginning. Thank you very much everyone for your time!

If it was me, I'd do this (fully tested):
// Original string
$str = 'The quick <font color="brown">brown</font> fox jumps over the lazy dog';
// Strip the font tag
$str = strip_tags( $str );
// Convert string to array
$arr = str_split( $str );
// Reverse the array
$rra = array_reverse( $arr );
// Convert array back to string
$str = implode( $rra );
// Add font tag back in
$str = str_replace('nworb', '<font color="brown">nworb</font>', $str);
// Result
echo $str;

Parse the HTML with something that will give you a DOM API to it.
Write a function that loops over the child nodes of an element.
If a node is a text node, get the data as a string, split it on words, reverse each one, then assign it back.
If a node is an element, recurse into your function.

Use preg_match_all() function.
$originalStr = "The quick <font color='brown'>brown</font> fox jumps over the lazy dog";
preg_match_all('|<[^>]+>(.*)</[^>]+>|U', $originalStr, $matches, PREG_SET_ORDER, 0);
$_tag = $matches[0][0];
$_txt = $matches[0][1];
$newString = str_replace($_tag,$_txt,$originalStr);

Related

PHP : add a html tag around specifc words

I have a data base with texts and in each text there are words (tags) that start with # (example of a record : "Hi I'm posting an #issue on #Stackoverflow ")
I'm trying to find a solution to add html code to transform each tag into a link when printing the text.
So the text are stored as strings in MySQL database like this :
Some text #tag1 text #tag2 ...
I want to replace all these #abcd with
#abcd
And have a final result as follow:
Some text #tag1 text #tag2 ...
I guess that i should use some regex but it is not at all my strong side.
Try the following using preg_replace(..)
$input = "Hi I'm posting an #issue on #Stackoverflow";
echo preg_replace("/#([a-zA-Z0-9]+)/", "<a href='targetpage.php?val=$1'>#$1</a>", $input);
http://php.net/manual/en/function.preg-replace.php
A simple solution could look like this:
$re = '/\S*#(\[[^\]]+\]|\S+)/m';
$str = 'Some text #tag1 text #tag2 ...';
$subst = '#$1';
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
Demo
If you are actually after Twitter hashtags and want to go crazy take a look here how it is done in Java.
There is also a JavaScript Twitter library that makes things very easy.
Try this the function
<?php
$demoString1 = "THIS is #test STRING WITH #abcd";
$demoString2 = "Hi I'm posting an #issue on #Stackoverflow";
function wrapWithAnchor($link,$string){
$pattern = "/#([a-zA-Z0-9]+)/";
$replace_with = '<a href="'.$link.'?val=$1">$1<a>';
return preg_replace( $pattern, $replace_with ,$string );
}
$link= 'http://www.targetpage.php';
echo wrapWithAnchor($link,$demoString1);
echo '<hr />';
echo wrapWithAnchor($link,$demoString2);
?>

PHP Split array at space before certain qty of characters

I have an application that can only deal with text up to 100 characters in length per line.
However I do not want to be splitting mid-word in a sentence as that doesn't look very nice. Therefore we would need to find the space before the 100th Character and then add it into the array.
I was thinking using strrpos would work - but am unsure how to do the continuing so it has everything in one array
$textToDraw = 'this is a message that is over 100 characters long just to see how well that the breaks work';
$characterLimit = substr($textToDraw, 0, 100);
$textBeforeLimit = strrpos($characterLimit, ' ', 0);
Thanks
UPDATE. This is the current code I have to split the text into an array and then draw each line. However I need it to cut on the space before 100 characters - and not on a hardcoded 100 character limit.
for ($i = 0; $i < count($textToDraw); $i++) {
$splitPoint = 100;
if ( strlen($textToDraw[$i]) > $splitPoint ) {
$newTextLines = str_split($textToDraw[$i], $splitPoint);
array_splice($textToDraw, $i, 1, $newTextLines);
$i = $i + count($newTextLines) - 1;
}
}
foreach ($textToDraw as $actualTextToDraw) {
$page->drawText($actualTextToDraw, $this->x , $this->y , 'UTF-8');
}
You can use php function wordwrap as below. This Wraps a string to a given number of characters using a string break character.
<?php
$textToDraw = 'this is a message that is over 100 characters long just to see how well that the breaks work';
$newtext = wordwrap($textToDraw, 100, "<br />\n");
echo $newtext;
?>
Try wordwrap().
Wraps a string to a given number of characters using a string break character.
Example adapted from the documentation page:
<?php
$text = "The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 20, "<br />\n");
echo $newtext;
// Outputs:
// The quick brown fox<br />
// jumped over the lazy<br />
// dog.
Update for new info in question:
Apply it to your needs:
$text = "Here's some example text that may or may not be really really long.";
$linedText = wordwrap($text, 20, "\n");
$lines = explode("\n", $linedText);
// Do whatever with $lines.

preg_replace_callback highlight pattern not match in result

I have this code:
$string = 'The quick brown fox jumped over the lazy dog and lived to tell about it to his crazy moped.';
$text = explode("#", str_replace(" ", " #", $string)); //ugly trick to preserve space when exploding, but it works (faster than preg_split)
foreach ($text as $value) {
echo preg_replace_callback("/(.*p.*e.*d.*|.*a.*y.*)/", function ($matches) {
return " <strong>".$matches[0]."</strong> ";
}, $value);
}
The point of it is to be able to enter a sequence of characters (in the code above it's a fixed pattern), and it finds and highlights those characters in the matched word. The code I have now highlights the entire word. I'm looking for the most efficient way of highlighting the characters.
The result of the current code:
The quick brown fox jumped over the lazy dog and lived to tell about it to his crazy moped.
What I would like to have:
The quick brown fox jumped over the lazy dog and lived to tell about it to his crazy moped.
Did I take the wrong approach? It would be awesome if someone could point me in the right way, I've been searching for hours and didn't find what I was looking for.
EDIT 2:
Divaka's been a great help. Almost there.. I apologize if I haven't been clear enough on what my goal is. I will try to explain further.
- Part A -
One of the things I will be using this code for is a phone book. A simple example:
When following characters are entered:
Jan
I need it to match following examples:
Jan Verhoeven
Arjan Peters
Raj Naren
Jered Von Tran
The problem is that I will be iterating over the entire phone book, person-record per person-record. Each person also has email-addresses, a postal address, maybe a website, a extra note, ect.. This means that the text I'm actually search can contain anything from letters, numbers, special characters(&#()%_- etc..), newlines, and most importantly spaces. So an entire record (csv) might contain the following info:
Name;Address;Email address;Website;Note
Jan Verhoeven;Veldstraat 2a, 3209 Herkstad;jan#werk.be;www.janophetwerk.be,jan#telemet.be;Jan die ik ontmoet heb op de bouwbeurs.\n Zelfstandige vertegenwoordiger van bouwmaterialen.
Raj Naren;Kerklaan 334, 5873 Biep;raj#werk.be;;Rechtstreekse contactpersoon bij Werk.be (#654 intern)
The \n is meant to be an actual newline. So if I search for #werk.be, I'd like to see both these records as a result.
- Part B -
Something else I want to use this for is searching song-texts. When I'm looking for a song and I can only remember it had to do something with ducks or docks and a circle, I would enter dckcircle and get the following result:
... and the ducks were all dancing in a great big circle, around the great big bonfire ...
To be able to fine-tune the searching I'd like to be able to limit the number of spaces (or any other character), because I would imagine it finding a simple pattern like eve in every song while I'm only looking for a song that has the exact word eve in it.
- Conclusion -
If I summarize this in pseudo-regex, for a search pattern abc with a max of 3 spaces in-between it would be something like this: (I might be totally off here)
(a)(any character, max 3 spaces)(b)(any character, max 3 spaces)(c)
Or more generic:
(a)({any character}{these characters with a limit of 3})(b)({any character}{these characters with a limit of 3})(c)
This can even be extended to this fairly easily I'm guessing:
(a)({any character}{these characters with a limit of 3}{not these characters})(b)({any character}{these characters with a limit of 3}{not these characters})(c)
(I know the ´{}´ brackets are not to be used that way in a regular expression, but I don't know how else to put it without using a character that has a meaning in regular expressions.)
If anyone wonders, I know the sql like statement would be able to do 80% (I'm guessing, might even be more) of what I'm trying to do, but I'm trying to avoid using a database to make this as portable as possible.
When the correct answer has been found, I'll clean this question (and the code) up and post the resulting php-class here (maybe I'll even put it up on github if that would be useful), so anyone looking for the same will have a fully working class to work with :).
I've came up with this. Tell me if it's what you want!
//$string = "The quick brown fox jumped over the lazy dog and lived to tell about it to his crazy moped.";
$string = "abcdefo";
//$pattern_array1 = array(a,y);
//$pattern_array2 = array(p,e,d);
$pattern_array1 = array(e,f);
$pattern_array2 = array(o);
$pattern_array2 = array(a,f);
$number_of_patterns = 2;
$regexp1 = generate_regexp($pattern_array1, 1);
$regexp2 = generate_regexp($pattern_array2, 2);
$string = preg_replace($regexp1["pattern"], $regexp1["replacement"], $string);
$string = preg_replace($regexp2["pattern"], $regexp2["replacement"], $string);
$string = transform_multimatched_chars($string);
// transforming other chars after transforming the multimatched ones
for($i = 1; $i <= $number_of_patterns; $i++) {
$string = str_replace("#{$i}", "<strong>", $string);
$string = str_replace("#/{$i}", "</strong>", $string);
}
echo $string;
function generate_regexp($pattern_array, $pattern_num) {
$regexp["pattern"] = "/";
$regexp["replacement"] = "";
$i = 0;
foreach($pattern_array as $key => $char) {
$regexp["pattern"] .= "({$char})";
$regexp["replacement"] .= "#{$pattern_num}\$". ($key + $i+1) . "#/{$pattern_num}";
if($key < count($pattern_array) - 1) {
$regexp["pattern"] .= "(?s)((?:(?!{$pattern_array[$key + 1]})(?!\s).)*)";
$regexp["replacement"] .= "\$".($key + $i+2) . "";
}
$i = $key + 1;
}
$regexp["pattern"] .= "/";
return $regexp;
}
function transform_multimatched_chars($string)
{
preg_match_all("/((#[0-9]){2,})(.*)((#\/[0-9]){2,})/", $string, $matches);
// change this for your purposes
$start_replacement = '<span style="color:red;">';
$end_replacement = '</span>';
foreach($matches[1] as $key => $match)
{
$string = str_replace($match, $start_replacement, $string);
$string = str_replace($matches[4][$key], $end_replacement, $string);
}
return $string;
}

How to parse this kind of HTML code with using PHP?

First of all, I found some threads here on SO, for example here, but it's not exactly what I am looking for.
Here is a sample of text that I have:
Some text bla bla bla bla<br /><b>Date</b>: 2012-12-13<br /><br /><b>Name</b>: Peter Novak<br /><b>Hobby</b>: books,cinema,facebook
The desired output:
2012-12-13
Peter Novak
books,cinema,facebook
I need to save this information into our database, but I don't know, how to detect between the <b> tags the value (eg. Date) and then immediately the value (in this case : 2012-12-13)...
I would be grateful for every help with this, thank you!
Since there's not much DOM to traverse, there's not much a DOM traversal tool can do with this.
This should work:
1) Remove everything before the b tag.
2) Remove the b tags. A DOM traversal tool can do this, but if they are pure text, even a regex can do it, and it can remove the colon and the subsequent whitespace in the same pass: <b\s*>[^<]+</b\s*>:\s*
3) Change sequences of br tags to bare newlines (do you really want to?). The DOM traversal tool can do this, but so can regexes: (?:<br\s*/?>)+
$html = preg_replace('#^[^<]+#', "", $html);
$html = preg_replace('#<b\s*>[^<]+</b\s*>:\s*#', "", $html);
$html = preg_replace('#(?:<br\s*/?>)+#', "\n", $html);
If <b>Date</b>, <b>Name</b>, <b>Hobby</b> and the <br />'s will always be there in that way, I suggest you use strpos() and substr().
For instance, to get the date:
// Get start position, +13 because of "<b>Date</b>: "
$dateStartPos = strpos($yourText, "<b>Date</b>") + 13;
// Get end position, use dateStartPos as offset
$dateEndPos = strpos($yourText, "<br />", $dateStartPos);
// Cut out the date, the length is the end position minus the start position
$date = substr($yourText, $dateStartPos, ($dateEndPos - $dateStartPos));
Assuming that the format is consistent, then explode can work for you:
<?php
$text = "Some text bla bla bla bla<br /><b>Date</b>: 2012-12-13<br /><br /><b>Name</b>: Peter Novak<br /><b>Hobby</b>: books,cinema,facebook";
$tokenized = explode(': ', $text);
$tokenized[1] = explode("<br", $tokenized[1]);
$tokenized[2] = explode("<br", $tokenized[2]);
$tokenized[3] = explode("<br", $tokenized[3]);
$date = $tokenized[1][0];
$name = $tokenized[2][0];
$hobby = $tokenized[3][0];
echo $date;
echo $name;
echo $hobby;
?>
Using PHP Simple HTML DOM Parser you can achieve this easily (just like jQuery)
include('simple_html_dom.php');
$html = str_get_html('Some text bla bla bla bla<br /><b>Date</b>: 2012-12-13<br /><br /><b>Name</b>: Peter Novak<br /><b>Hobby</b>: books,cinema,facebook');
Or
$html = file_get_html('http://your_page.com/');
then
foreach($html->find('text') as $t){
if(substr($t, 0, 1)==':')
{
// do whatever you want
echo substr($t, 1).'<br />';
}
}
The output of the example is given below
2012-12-13
Peter Novak
books,cinema,facebook

PHP Wordwrap on an angle, increasing indentation each break

is it possible with php's wordwrap to add increased indentation each line break to essentially create wordwrapping on an angle?
If I understand your question correcly, you would like to produce an output like:
xxxxxxx
xxxxxxxxx
xxxxxxxxxxx
xxxxxxxxxxxxx
xxxxxxxxxxxxxxx
Obviously, replacing x with your text.
wordwrap built-in function does not support this feature but you still can write your own, with a simple loop. Change the max length on each iteration, and break your initial string (depending on your needs, where you find a space or wherever you want).
<?php
$text = "The quick brown fox jumped over the lazy dog.";
echo $return = costomwrap($text , 10);
function costomwrap($text , $len)
{
$str = '';
for($i=0;$i<strlen($text);$i = $i+$len)
{
$str .= substr($text , $i , $len ).'<br />';
$len--;
}
return $str;
}
?>
live demo http://codepad.org/v3ysqV5A.
here, <br /> not come on your programme.

Categories