Viewed   78 times

I'm trying to find a way to make a list of everything between <a> and </a> tags. So I have a list of links and I want to get the names of the links (not where the links go, but what they're called on the page). Would be really helpful to me.

Currently I have this:

$lines = preg_split("/r?n|r/", $content);  // content is the given page
foreach ($lines as $val) {
  if (preg_match("/(<A(.*)>)(</A>)/", $val, $alink)) {     
    $newurl = $alink[1];

    // put in array of found links
    $links[$index] = $newurl;
    $is_href = true;



The standard disclaimer applies: Parsing HTML with regular expressions is not ideal. Success depends on the well-formedness of the input on a character-by-character level. If you cannot guarantee this, the regex will fail to do the Right Thing at some point.

Having said that:

<ab[^>]*>(.*?)</a>   // match group one will contain the link text
Tuesday, September 6, 2022

The Arabic regex is:


Actually, ?-? is a subset of this Arabic range, so I think you can remove them from the pattern.

So, in JS it will be


See regex demo

Tuesday, October 11, 2022

For this PHP regex:

$str = preg_replace ( '{(.)1+}', '$1', $str );
$str = preg_replace ( '{[ '-_()]}', '', $str )

In Java:

str = str.replaceAll("(.)\1+", "$1");
str = str.replaceAll("[ '-_\(\)]", "");

I suggest you to provide your input and expected output then you will get better answers on how it can be done in PHP and/or Java.

Sunday, October 9, 2022

If you're annoyed about these tags being recreated when you run git pull, you turn off the fetching of tags by default with the remote.<remote-name>.tagopt config setting. e.g. if the remote is origin, then you can do:

git config remote.origin.tagopt --no-tags

Update: to address your comment, the reason that I suggest this is that there's not an obvious way to tell the difference between a tag that was created locally and one that was fetched from a remote. There's also no reflog for tags. So, my suggestion is to suppress automatic fetching of tags - you can then fetch them yourself into a different namespace. For example, you could do:

git fetch origin +refs/tags/*:refs/tags/origin/*

... and perhaps create an alias for that. Then when you want to fetch tags, they'll be named, for example, refs/tags/origin/tag1 instead of refs/tags/tag1.

If you want this to happen automatically, you could change your .git/config to list multiple refspecs for fetching, e.g.:

 [remote "origin"]
      url = whoever@whereever:whatever.git
      fetch = +refs/heads/*:refs/remotes/origin/*
      fetch = +refs/tags/*:refs/tags/origin/*

... which is suggested in Pro Git.

Friday, December 9, 2022

This was pretty fun to work on.

You have access to all of the styles on a page through document.styleSheets, so you just need scope the rules on a style. So lets say that I have this class:

.class {
    font-size: 20px;
    color: blue;

How jsfiddle implements sheets, this is the third style sheet added to the document, so I can just assign to the scope like this:

myApp.controller('TestController', ['$scope', function ($scope) {
    $scope.styles = document.styleSheets[3].rules;     

This would let you do things like $scope.styles[0].style['color'] = 'red' to change the color of anything with class to red. Since it's the first thing in the style array.

But that's not cool enough, so we want to create a directive where we can change these from a ui. So we'd like to know all of the things that class controls, to create controls for them, So we can manipulate the css string to get all of those.

Next we have to create a temporary scoped object on the directive that starts off with all of the styles. The reason is that style sheets have checking, so as you type into an input if you do something like $scope.styles[0].style['color'] = 'g' and it was red, it will just reset to red.

So we create an input for each style type, with ng-model of our temp, then just listen for changes and attempt to assign to the style sheet.

I created a fiddle where I implement it, but the directive looks like this.

myApp.directive('styler', function() {
    return {
        scope: {
            styles: '='
        restrict: 'EA',
        template: '<div ng-repeat="type in types">{{type}} <input ng-change="Change(type)" ng-model="temp_styles[type]"/></div>',
        link: function(scope, elt, attrs) {      
            // get the actual styles
            var types = scope.styles.cssText.replace(/ /g, '').split('{')[1].split('}')[0].split(';');
            scope.types = [];
            scope.temp_styles = {};
            // get rid of "" element at the end
            for (var i in types) {
                var split = types[i].split(':');
                scope.temp_styles[split[0]] = split[1];
            scope.Change = function(type) {
      [type] = scope.temp_styles[type];

Cool, dynamic two way binding of styles!

Hope this helped!

Wednesday, November 2, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :