I have:
$regExge = "/(?!((<.*?)))\b(Öffnung)\b(?!(([^<>]*?)>))/s";
$replacege = "<a href=''>Öffnung</a>";
And I used preg_replace to replace Öffnung string to html <a href=''>Öffnung</a>
$content = preg_replace($regExge, $replacege, $content);
But it not working, only working with normal string.
There any way? Thank you.
You need to indicate that your pattern should cover an encoding which includes the special characters. UTF-8 is one option here, and can be indicated by using /u at the end of the pattern:
$regExge = "/(?!((<.*?)))\b(Öffnung)\b(?!(([^<>]*?)>))/su";
$replacege = "<a href=''>Öffnung</a>"; // ^^^
$content = preg_replace($regExge, "stuff", $replacege);
print $content;
<a href=''>stuff</a>
Demo
You can try this modified version.
$content = "Offnung Öffnung s;ldjfjkasÖffnung";
$regExge = "/Öffnung/";
$replacege = "<a href=''>Öffnung</a>";
$content = preg_replace($regExge, $replacege, $content);
echo $content;
Related
how can i get only text data from the table? Without any links and images?
while ($row = $most_rep->fetch_assoc()) {
$post_title = $row['post_title'];
$post_content = mb_substr($row['post_content'],0,120);
$guid = $row['guid'];
$post_date = $row['post_date'];
echo
"<a href='$guid' target='_blank'>"
."<div class='post_title'>$post_title</div>"."</a>"
."<a href='$guid' target='_blank'>"
."<div class='post_description'>$post_content</div>"
."<div class='post_date'>$post_date</div>"
."</a>";
}
Inside variable $post_content i have text and images and links from the post. I would like to echo only text.
Appreciate any help
use 'strip_tags' function
http://php.net/manual/en/function.strip-tags.php
for example ,
$only_text = strip_tags($post_content);
it is not the best practice but if you still insist using this kind of approach i suggest add some Delimiter to your data $row['post_content'] and use explode(" ",$row['post_content']) to get the specific text or value that you want.
Note: the explode function will return an array.
You can use PHP Filter Sanitization:
<?php
$str = "test";
$newstr = filter_var($str, FILTER_SANITIZE_STRING);
echo $newstr;
?>
Try this to remove everything except a-z, A-Z and 0-9:
$result = preg_replace("/[^a-zA-Z0-9]+/", "", $s);
If your definition of alphanumeric includes letters in foreign languages and obsolete scripts then you will need to use the Unicode character classes.
Try this to leave only A-Z:
$result = preg_replace("/[^A-Z]+/", "", $s);
The reason for the warning is that words like résumé contains the letter é that won't be matched by this. If you want to match a specific list of letters adjust the regular expression to include those letters. If you want to match all letters, use the appropriate character classes as mentioned in the comments.
I want to echo php text but not between (). Some thing like this =
<?php
$text = "Barry(male)";
echo $text;
?>
output =
Barry
How can i do this?
You can use preg_replace to substitute whatever is between parenthes (and the parentheses themselves) with an empty string. Like this:
<?php
$text = "Barry(male)";
echo preg_replace('#\(.*\)#', '', $text);
?>
Please note: since you didn't specify your string format, I'm assuming that the parenthesized text appears just once in the string and that there aren't nested parenthes. Otherwise, this doesn't work as expected.
Something like:
$text = "Barry(male)";
$split = explode("(", $text);
echo $split[0];
// "Barry"
I have a string, that look like this "<html>". Now what I want to do, is get all text between the "<" and the ">", and this should apply to any text, so that if i did "<hello>", or "<p>" that would also work. Then I want to replace this string with a string that contains the string between the tags.
For example
In:
<[STRING]>
Out:
<this is [STRING]>
Where [STRING] is the string between the tags.
Use a capture group to match everything after < that isn't >, and substitute that into the replacement string.
preg_replace('/<([^>]*)>/, '<this is $1>/, $string);
here is a solution to test on the pattern exists and then capture it to finally modify it ...
<?php
$str = '<[STRING]>';
$pattern = '#<(\[.*\])>#';
if(preg_match($pattern, $str, $matches)):
var_dump($matches);
$str = preg_replace($pattern, '<this is '.$matches[1].'>', $str);
endif;
echo $str;
?>
echo $str;
You can test here: http://ideone.com/uVqV0u
I don't know if this can be usefull to you.
You can use a regular expression that is the best way. But you can also consider a little function that remove first < and last > char from your string.
This is my solution:
<?php
/*Vars to test*/
$var1="<HTML>";
$var2="<P>";
$var3="<ALL YOU WANT>";
/*function*/
function replace($string_tag) {
$newString="";
for ($i=1; $i<(strlen($string_tag)-1); $i++){
$newString.=$string_tag[$i];
}
return $newString;
}
/*Output*/
echo (replace($var1));
echo "\r\n";
echo (replace($var2));
echo "\r\n";
echo (replace($var3));
?>
Output give me:
HTML
P
ALL YOU WANT
Tested on https://ideone.com/2RnbnY
Say I have the following text
..(content).............
<A HREF="http://foo.com/content" >blah blah blah </A>
...(continue content)...
I want to delete the link and I want to delete the tag (while keeping the text in between). How do I do this with a regular expression (since the URLs will all be different)
Much thanks
This will remove all tags:
preg_replace("/<.*?>/", "", $string);
This will remove just the <a> tags:
preg_replace("/<\\/?a(\\s+.*?>|>)/", "", $string);
Avoid regular expressions whenever you can, especially when processing xml. In this case you can use strip_tags() or simplexml, depending on your string.
<?php
//example to extract the innerText from all anchors in a string
include('simple_html_dom.php');
$html = str_get_html('<A HREF="http://foo.com/content" >blah blah blah </A><A HREF="http://foo.com/content" >blah blah blah </A>');
//print the text of each anchor
foreach($html->find('a') as $e) {
echo $e->innerText;
}
?>
See PHP Simple DOM Parser.
Not pretty but does the job:
$data = str_replace('</a>', '', $data);
$data = preg_replace('/<a[^>]+href[^>]+>/', '', $data);
strip_tags() can also be used.
Please see examples here.
$pattern = '/href="([^"]*)"/';
I use this to replace the anchors with a text string...
function replaceAnchorsWithText($data) {
$regex = '/(<a\s*'; // Start of anchor tag
$regex .= '(.*?)\s*'; // Any attributes or spaces that may or may not exist
$regex .= 'href=[\'"]+?\s*(?P<link>\S+)\s*[\'"]+?'; // Grab the link
$regex .= '\s*(.*?)\s*>\s*'; // Any attributes or spaces that may or may not exist before closing tag
$regex .= '(?P<name>\S+)'; // Grab the name
$regex .= '\s*<\/a>)/i'; // Any number of spaces between the closing anchor tag (case insensitive)
if (is_array($data)) {
// This is what will replace the link (modify to you liking)
$data = "{$data['name']}({$data['link']})";
}
return preg_replace_callback($regex, array('self', 'replaceAnchorsWithText'), $data);
}
use str_replace
I'm using the following code to add <span> tags behind <a> tags.
$html = preg_replace("~<a.*?href=\"$url\".*?>.*?</a>~i", "$0<span>test</span>", $html);
The code is working fine for regular links (ie. http://www.google.com/), but it will not perform a replace when the contents of $url are $link$/3/.
This is example code to show the (mis)behaviour:
<?php
$urls = array();
$urls[] = '$link$/3/';
$urls[] = 'http://www.google.com/';
$html = 'Test Link' . "\n" . 'Google';
foreach($urls as $url) {
$html = preg_replace("~<a.*?href=\"$url\".*?>.*?</a>~i", "$0<span>test</span>", $html);
}
echo $html;
?>
And this is the output it produces:
Test Link
Google<span>test</span>
$url = preg_quote($url, '~'); the dollar signs are interpreted as usual: end-of-input.
just somebody is correct; you must escape your special regex characters if you mean for them to be interpreted as literal.
It also looks to me like it can't perform the replace because it never makes a match.
Try replacing this line:
$urls[] = '$link$/3/';
With this:
$urls[] = '$link/3/';
$ is considered a special regex character and needs to be escaped. Use preg_quote() to escape $url before passing it to preg_replace().
$url = preg_quote($url, '~');
$ has special meaning in regex. End of line. Your expression is being expanded like this:
$html = preg_replace("~<a.*?href=\"$link$/3/\".*?>.*?</a>~i", "$0<span>test</span>", $html);
Which fails because it can't find "link" between two end of lines. Try escaping the $ in the $urls array:
$urls[] = '\$link\$/3/';